Sign in to follow this  

weird c++ delete behavior

Recommended Posts

When deleting pointers to my Image class I noticed something strange. I had this: delete bgImg, bgImg2; Where bgImg and bgImg2 were of type Image*. But when I stepped through the line in debug mode I noticed that only bgImg's destructor was begin called! bgImg2 was "cleaned" up memory-wise, but since it's destructor wasn't called, it was leaking. Strangely enough, changing the line to this: delete bgImg; delete bgImg2; Fixed the problem! I thought deletes guaranteed a destructor call.

Share this post

Link to post
Share on other sites
I'm no syntax expert, but I believe the first only calls delete for bgImg. You need to say:
delete bgImg;
delete bgImg2;

because these are two seperate statements. Each statement must end in a ;

It's similar to how

float x,y =0;
will only initialize y to 0, not x (correct me if I'm wrong, but I believe that's how it works with my compiler).

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