Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QScopedPointer and QSharedPointer don't always solve the memory management problem

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 7.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VC15
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tony
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        VC15
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • B Offline
          B Offline
          baysmith
          wrote on last edited by
          #4

          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
          @

          Nokia Certified Qt Specialist.

          1 Reply Last reply
          0
          • V Offline
            V Offline
            VC15
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • B Offline
              B Offline
              baysmith
              wrote on last edited by
              #6

              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)

              Nokia Certified Qt Specialist.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved