QScopedPointer and QSharedPointer don't always solve the memory management problem



  • I had a memory leak in my Qt application. All operations with memory allocation and releasing are made with QScopedPointer and QSharedPointer. I spent much time finding the "hole". After 2 days of debugging I found what was the problem. I had a code similar to the following.
    @
    class A
    {
    public:
    virtual void f() = 0;
    };

    class B : public A
    {
    private:
    int* m_buf;
    public:
    B(int n)
    {
    m_buf = new int[n];
    }
    virtual ~B()
    {
    delete[] m_buf;
    }
    virtual void f()
    {
    ...
    }
    };
    ...
    // Somewhere in the program
    A* a = new B;
    ...
    delete a;@
    This is not the actual code. It is an example to explain the problem. After calling delete the memory pointed by m_buf didn't get deleted. The reason is that class A doesn't have a virtual destructor it only has the default non-virtual destructor.

    Thus if you create a class for inheritance always make its destructor virtual so that you won't face such a problem.



  • Well,

    our Master Of The Universe, Bjarne Stroustrup, already taught us that:

    "Why are destructors not virtual by default?":http://www2.research.att.com/~bs/bs_faq2.html#virtual-dtor

    Anyway, it happens to (almost) everybody at least once in one's life :)

    Antonio.



  • Oh, what a good FAQ! Thank you for this link, Antonio!



  • If you are using a version of GCC which supports it, you might want to consider the -Weffc++ option which will display a warning about the problem (and other potential issues).

    @

    g++ -Weffc++ tmp.cpp
    tmp.cpp:4: warning: ‘class A’ has virtual functions and accessible non-virtual destructor
    tmp.cpp:11: warning: ‘class B’ has pointer data members
    tmp.cpp:11: warning: but does not override ‘B(const B&)’
    tmp.cpp:11: warning: or ‘operator=(const B&)’
    tmp.cpp: In constructor ‘B::B(int)’:
    tmp.cpp:15: warning: ‘B::m_buf’ should be initialized in the member initialization list
    @



  • I use MinGW 4.5 with -Wall option but didn't get such messages. Looks like that this option isn't enough.

    I have just executed @g++ --help=warnings@ -Wall is said to "Enable most warning messages". Most is not all. So I have to add some other options (e.g. -Weffc++) manually, don't I?



  • Correct.

    bq. GCC provides many other warning options that are not included in -Wall but are often useful. Typically these produce warnings for source code which may be technically valid but is very likely to cause problems. The criteria for these options are based on experience of common errors -- they are not included in -Wall because they only indicate possibly problematic or "suspicious" code. (From "An Introduction to GCC - for the GNU compilers gcc and g++ by Brian J. Gough":http://www.network-theory.co.uk/docs/gccintro/gccintro_31.html)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.