• Advertisement
Sign in to follow this  

weird c++ delete behavior

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

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
Advertisement
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
You could of course go,

delete bgImage, delete bgImage2;

if you really wanted ;)

-Sirp.

Share this post


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

  • Advertisement