Archived

This topic is now archived and is closed to further replies.

WhiteRaptor

DirectDraw Surface Question

Recommended Posts

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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites