Skip to main content
GameDev.net gamedev.net
🔒 Locked

DirectDraw Surface Question

Started by WhiteRaptor Dec 10, 2001 at 12:21 AM 3 replies 200+ views
Original Post
WhiteRaptor
WhiteRaptor
When dealing with Direct Draw surfaces (in C++), Let's say there's a class, and inside the class is the variable LPDIRECTDRAWSURFACE7 ClassSurface. Now, in the class's constructor function one of the variables passed into it is (LPDIRECTDRAWSURFACE7 SomeSurface). Your goal is to have "ClassSurface" point to whatever surface is supplied by the user (via "SomeSurface")in the creation of the class. For instance, you might supply the primary surface to the function. So, inside the constructer function you have a line like: ClassSurface = SomeSurface; Now, my question is A) is this proper? B) should the "ClassSurface" be in some way released or destroyed in the destructor (in a similar way that you would with the primary surface before your program exits)? "ClassSurface" seems to be a pointer-to-a-pointer, or a second pointer to the same surface, or something in this case and my fear is that if "ClassSurface" is made to point to the primary surface (which already has a pointer somewhere else), and is released, it will cause problems when you try to use the primary surface elsewhere with the original primary surface pointer. Whew, ok, did I make my question clear? If not let me know. I hope I made sense - thanks in advance. Edited by - WhiteRaptor on December 10, 2001 2:27:02 AM
FantasyGuy
FantasyGuy
A) Is this proper?
It depends on your engine''s design. There is no problem in such a design anyway.

B) Should ClassSurface be released?
Of course. Every COM class should be released after finishing with it. The number of Release calls should be equal to the reference count of the class. So, if you have put ClassSurface->AddRef() in the constructor, then there should be ClassSurface->Release() in the destructor (in addition to the Release call in the main program).
CaptainJester
CaptainJester
To answer ''B'', no you should not release it within the class. If you create a class, it should only release anything that it alocates. If, as in this case, a DirectDraw surface is passed into a class, whether it is through the constructor or a method, you are not the controller of the pointer, you are only allowed access to it. If you release it inside the class, then outside the class, someone may still try to use it as it was created outside the class. In other words, only release a reference to something in the same area that it was created.

---
Make it work.
Make it right.
Make it fast.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
grasshopa55
grasshopa55
To respond to CaptainJester, it depends on how he is storing the Surface, if he is just storing a pointer to the surface, not copying, it depends on how he wants ownership of that pointer to be handled. When you pass the LPDIRECTDRAWSURFACE to your class, if your design says that the class now takes control of that pointer, then the class should copy that pointer into it, and the calling program should delete it''s reference to the surface, since it is now stored in the class. If you want the calling program to maintain the surface, then you do not delete it in the class, but then this would defeat the purpose of using classes in the first place. Just a suggestion.

-----------------------------
kevin@mayday-anime.com
http://games.mayday-anime.com
-----------------------------kevin@mayday-anime.comhttp://www.mayday-anime.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.