Jump to content
  • Advertisement
Sign in to follow this  
Evil Booger

C++ Class Destructor not working

This topic is 3911 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

Ok here’s the story, I finished a demo for my game, but I noticed that the stand-alone exe got an error message when I close out of it so I decided to check out my class destructors. I did that by including iostream and adding a std::cout<<Destructing ship class<<std::endl; line at the end of the destructor. I don’t know the official name for this type of program but my program runs with the windows command prompt thingy behind it and it displays my cout messages. Anyways, the destructor message never shows, but my similar constructor messages show. How can I fix this? The classes are declared at the top of my main file (not in a function) so does that stop the destructor from running? And this is a real problem because my destructors are supposed to clean up the files I loaded. *gasp* Have I been leaking memory for the past 6 months! If you need to see some code just tell me because I don’t know what I could give you to help.

Share this post


Link to post
Share on other sites
Advertisement
The destructor should be called when the object is destroyed or out of scope. If it goes out of scope when the program ends the program might be closing before you cans see the destructor message. Code would certainly help to see what going on.

Share this post


Link to post
Share on other sites
The command line stays up even when the program closes and I have to close that separately by clicking the little 'x' so I think that it would show it if I used a destructor.

What code would you like? The actual destructor?


Ship::~Ship()
{
if(!surface)
{
SDL_FreeSurface(surface);
}
if(!altsurface)
{
SDL_FreeSurface(altsurface);
}
if(!explosionsurface)
{
SDL_FreeSurface(explosionsurface);
}
std::cout<<"Destroying a ship"<<std::endl;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Booger
The command line stays up even when the program closes and I have to close that separately by clicking the little 'x' so I think that it would show it if I used a destructor.

What code would you like? The actual destructor?

*** Source Snippet Removed ***


My guess would be that the error is happening before the output line in the destructor. Take a closer look at the conditional statements around each SDL_FreeSurface call. You might want to invert those if statements. Right now it will only try to free a surface if it == 0 (NULL).

If any of those surface pointers is still NULL, when you attempt to free it the program will crash.

Also, the surface pointers that need to be freed will not be freed, and you will still be leaking memory. (The leaked memory will be taken care of as soon as the program closes anyway, but still.)


Ship::~Ship()
{
std::cout << "Entering Ship destructor." << std::endl; // The extra line of output will tell you if the program crashes between this line and the bottom line of the constructor.
if(surface)
{
SDL_FreeSurface(surface);
}
if(altsurface)
{
SDL_FreeSurface(altsurface);
}
if(explosionsurface)
{
SDL_FreeSurface(explosionsurface);
}
std::cout << "Exiting Ship destructor." << std::endl;
}


Share this post


Link to post
Share on other sites
Quote:

What code would you like? The actual destructor?

The context where the ship object is constructed/deleted.

Does the ship object inherit a base class that does not have a virtual destructor?
Could it be that the ship instance you are deleting is contained/referenced by a object/singleton that is already deleted?

Share this post


Link to post
Share on other sites
Well pulpfist, I have an "enemyship" class that is derived from my "ship" class.

And the classes are declared outside of any functions so I'm not sure about their context of their deletion.

Share this post


Link to post
Share on other sites
When is SDL_Quit() being called? You probably shouldn't call any SDL functions after this point. If it is being called by atexit you should be very careful, as Im not sure whether global destructors are called before or after atexit functions.

In any case the best solution is not to have these objects as globals anyway, you can probably make them local to main without a huge amount of work.

Finally, a direct quote from the SDL doc wiki:
Quote:
It is safe to pass NULL as surface, in which case SDL_FreeSurface will do nothing.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Booger
Well pulpfist, I have an "enemyship" class that is derived from my "ship" class.


if your Ship destructor is not declared virtual you will be leaking then (depending a little on how you call things... but it really should be virtual)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!