Jump to content
  • Advertisement

Archived

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

vbisme

Out of Scope Precedence

This topic is 6260 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Let''s say I have too classes, they are dumb and serve for demo propose only:
  
clase Surfaces
{
LPDIRECTDRAWSURFACE lpPrimary;
LPDIRECTDRAWSURFACE lpBackbuffer;

Surfaces(){};
~Surfaces(){};
};
Surfaces::Surfaces()
{
lpPrimary = NULL;
lpBackbuffer = NULL;
}
Surfaces::~Surfaces()
{
if(lpBackbuffer) lpBackbuffer->Release();
if(lpPrimary) lpPrimary->Release();

class DDObj
{
LPDIRECTDRAW lpDD;

DDObj(){};
DDObj(){};
};

DDObj::DDObj()
{
lpDD = NULL;
}
DDObj::~DDObj()
{
if(lpDD) lpDD->Release();
}

//The main program

Surfaces mySurfaces;
DDObj myDD;

  
now when the program ends, they too classes goes out of scope and their destructors are called. Which are first? I wouldn''t want to release the DirectDraw Object before the Surfaces right?

Share this post


Link to post
Share on other sites
Advertisement
I don''t really know the answer, but I think you cannot know beforehand which will be deleted first. But there are two ways to solve this.


First, you could use pointers like this:
Surfaces* mySurfaces = new Surfaces;
DDObj* myDD = new DDObj;

and at the end of your program, you just delete them in the order you want to:
delete mySurfaces;
delete myDD;


And second, why won''t you just move the two classes into one larger class containing your DirectDraw object and surfaces together?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Things get destroyed in the reverse order they are constructed.

If in your main code, you have this:

DDObj myDD;
Surfaces mySurfaces;

The DDObj will get constructed first and destroyed last. Of course if you have them at global level in different files, you can''t know which order they''ll get destroyed.

I hope this helps,
- Pete

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!