Jump to content
  • Advertisement
Sign in to follow this  
Thylz

C++ and destructors

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

Hi!

Somtimes, when I'm testing a program made it with C/C++, this one crashes with a glibc message that says:

"Double free or corruption"

I solve this, usually, commenting any line in any destructor helped by gdb. Well, I suposse that this occurs when I'm setting free memory that the system already set free, it's true? How can I know when I must set free memory and when no?

Thanks and excuse my english.

Share this post


Link to post
Share on other sites
Advertisement
The issue is that you didn't follow the "rule of three". You need a copy constructor and operator= in there to handle the cases where you copied the class. Or use a "smart pointer" instead of a raw pointer. It isn't the delete that is bad. It is the fact that you aren't properly copying your pointed to objects.

Quote:

How can I know when I must set free memory and when no?

You always have to free any memory you allocate on the heap, or it is a leak.

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!