Memory Leak

Hi,

Below is a link to a portion of code where there is apparently a memory leak.

http://img585.imageshack.us/img585/1769/leakw.png

The code is from a 2007 past exam paper (pdf) so I can't paste it in code tags. Where is the leak? I can't find it.

Thanks
I'm not sure, but could it be that delete treats the Manager's as if they were simple Employee's?

Edit: Yep. You can find the better explanation here "Destructor pitfalls":
http://www.horstmann.com/cpp/pitfalls.html
Last edited on
Catfish is right
the leak must be the super class employee. A child class it depends on the parent. It looks like the parent needs to be destroyed as well.
Edit: Yep. You can find the better explanation here "Destructor pitfalls":
http://www.horstmann.com/cpp/pitfalls.html


Goodness me, look at that. it's the exact same code :)
Wow that is an amazing find!

Hmmmm just having a look at what the article said:

Pitfall:
delete staff[i];

destroys all objects with~Employee(). The_dept strings of the Manager objects are never destroyed.

Moral: A class from which you derive must have a virtual destructor.


So is it saying that if the destructor of the child class is not virtual, then mandatory implementation of the parent destructor will occur? I.e. the destructor needs to be virtual to override the parent destructor? Surely then you need to explicitly declare them both virtual?

Thanks for the help!
Last edited on
I think that virtual has a different meaning when applied to destructors.
I think it means: "also call the destructor of the Base class when destroying the Derived class".


Edit3: cleanup. (And you may quote me on the above anytime you need to prove I'm a moron.)
Last edited on
Topic archived. No new replies allowed.