On Fri, Jul 25, 2008 at 5:28 PM, Matthias Troyer <troyer@phys.ethz.ch> wrote:
On 25 Jul 2008, at 13:53, Brad Anderson wrote:

I'm getting memory leaks when deserializing objects and I can't figure out what I'm doing wrong.  I'm using VC++ 2005. I've boiled it down to the following code:

Just a quick guess:


   //virtual ~Base() { }  // uncommmenting this causes leaks to disappear

The base class needs a virtual destructor, thus uncommenting this is needed.
 
I solved my problem seconds before I got your message :).  I had actually done that in what I thought was the class giving issue but it turns out I had several base classes that were missing virtual destructors so that fix didn't appear to work because I was still getting tons of leaks.

I am curious, though, about why the leaks only happened in when I deserialized and not in any of the other instances I created myself.  Does shared_ptr perform some kind of trickery to detect the type to delete through, even when the shared_ptr is typed for the base class?


Matthias