[Serialization, Signals?] Memory leaks while deserializing derived type
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:
#include <fstream>
// include headers that implement a archive in simple text format
#include
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. Matthias
On Fri, Jul 25, 2008 at 5:28 PM, Matthias Troyer
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
AMDG Brad Anderson wrote:
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?
Yes. A shared_ptr captures the type to delete through when it is constructed. In Christ, Steven Watanabe
On Fri, Jul 25, 2008 at 11:47 PM, Steven Watanabe
AMDG
Brad Anderson wrote:
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?
Yes. A shared_ptr captures the type to delete through when it is constructed.
In Christ, Steven Watanabe
Good to know. Thanks.
participants (3)
-
Brad Anderson
-
Matthias Troyer
-
Steven Watanabe