Sign in to follow this  
Thylz

C++ and destructors

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this