Thursday, May 15, 2008

valgrind non-virtual dstor leak bug

I ran valgrind on the following code which, you'll see, leaks the data stored in the derived class.

Valgrind does not seem to notice the leak. I'm not so happy about that.

/// test.cc
#include

class Base {
public:
virtual void blah() = 0;
~Base() { std::cout << "Base dstor" << std::endl; }
private:
int data_;
};

class Derived : public Base
{
public:
virtual void blah() {}
~Derived() { std::cout << "Derived dstor" << std::endl;}
private:
int data2_;
int data3_;
};


int main()
{
Base * bptr = new Derived;
delete bptr;
return 0;
}

..........
From the command line.

>g++ test.cc -o test.out
>valgrind --tool=memcheck test.out
==31535== Memcheck, a memory error detector.
==31535== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
==31535== Using LibVEX rev 1575, a library for dynamic binary translation.
==31535== Copyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP.
==31535== Using valgrind-3.1.1, a dynamic binary instrumentation framework.
==31535== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
==31535== For more details, rerun with: -v
==31535==
Base dstor
==31535==
==31535== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 5 from 1)
==31535== malloc/free: in use at exit: 0 bytes in 0 blocks.
==31535== malloc/free: 1 allocs, 1 frees, 24 bytes allocated.
==31535== For counts of detected errors, rerun with: -v
==31535== All heap blocks were freed -- no leaks are possible.

No comments: