|
From: Aleksey \AlekSi\ P. <ale...@gm...> - 2009-08-29 15:53:05
|
Hi list,
Is it possible to detect memory leaks with Memcheck in the code below?
--
class Singleton
{
public:
static Singleton* instance()
{
if(!instance_)
instance_ = new Singleton();
return instance_;
}
private:
Singleton() { }
~Singleton() { }
private:
static Singleton* instance_;
};
Singleton* Singleton::instance_ = 0;
int main()
{
Singleton::instance();
return 0;
}
--
If answer is yes – how can I do it? If no – why? This is definitely a
leak in a heap.
Thanks,
AlekSi.
|
|
From: Tom H. <to...@co...> - 2009-08-29 16:17:46
|
On 29/08/09 16:52, Aleksey "AlekSi" Palazhchenko wrote: > If answer is yes – how can I do it? If no – why? This is definitely a > leak in a heap. No it isn't - there is still a static variable pointing at the heap object when the program exits. Tom -- Tom Hughes (to...@co...) http://www.compton.nu/ |
|
From: John R. <jr...@bi...> - 2009-08-29 17:32:41
|
>> If answer is yes – how can I do it? If no – why? This is definitely a >> leak in a heap. > > No it isn't - there is still a static variable pointing at the heap > object when the program exits. The destructor should set the static pointer to NULL. -- |
|
From: Christophe-Marie D. <chm...@gm...> - 2009-08-29 17:35:03
|
In the leak summary, you should see something like: ==9542== LEAK SUMMARY: ==9542== definitely lost: 0 bytes in 0 blocks. ==9542== possibly lost: 0 bytes in 0 blocks. ==9542== still reachable: 16,884 bytes in 685 blocks. ==9542== suppressed: 0 bytes in 0 blocks. I'm quoting Tim Post in the thread http://sourceforge.net/mailarchive/message.php?msg_name=1248351309.7845.202.camel%40localhost.localdomain "Still reachable means just that, at the time the program exited, pointers to the heap blocks existed. This usually means memory allocated in main() was not freed when the program terminated, the program just relied on the OS to reclaim the memory. Its not exactly the best practice to do that, but not a 'leak' by definition." So I think you'should get the information you want by looking at "still reachable" loss reccords. -- Christophe-Marie Duquesne |