Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qt memory management. What's wrong?
Forum Updated to NodeBB v4.3 + New Features

Qt memory management. What's wrong?

Scheduled Pinned Locked Moved Mobile and Embedded
9 Posts 5 Posters 7.6k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    sergey.pakhandrin
    wrote on last edited by
    #1

    Hello everyone!

    I have a question about memory leaks in Qt.

    I have a QMainWindow with 2 QPushButtons.

    First button click signal:

    @m_label = new QLabel(this);

    QPixmap pix(this->size());
    QPainter painter;
    painter.begin(&pix);

    painter.drawPixmap(this->rect(), QPixmap("1.png"));
    m_label->setPixmap(pix);

    painter.end();@

    Secont button click signal:

    @delete m_label;@
    When I start my test application the allocated memory is about 11900 Kb When I click on first button then allocated memory for app is about 12450 Kb When I click on the second button I got allocated memory about 12250 Kb

    Why I didn't get the same 11900 Kb? Is this a leak?

    So if to write the following code:

    @QImage img("1.png");
    QImage img1("1.png");
    QImage img2("1.png");
    QImage img3("1.png");
    QImage img4("1.png");
    QImage img5("1.png");
    QImage img6("1.png");
    QImage img7("1.png");
    QImage img8("1.png");
    QImage img9("1.png");@
    Then allocated memory grows but doesn't decrease. Why? How to clean this memory leak?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DenisKormalev
      wrote on last edited by
      #2

      How do you check memory used by process? Via Windows task manager? If yes than you should avoid it because it can show only rude values and it cannot be used to detect memory leeks.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        danilocesar
        wrote on last edited by
        #3

        Use properly apps, such as valgrind, to detect memory leeks. Common tools, such as top, are not very good to detect leeks.

        <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
        Software Engineer

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DenisKormalev
          wrote on last edited by
          #4

          Yes, danilocesar is right about valgrind (memcheck tool).

          1 Reply Last reply
          0
          • E Offline
            E Offline
            espringe
            wrote on last edited by
            #5

            @m_label = new QLabel(this);@

            What that does, is creates a new QLabel, stores the address of the newly created label in m_label -- and the (this) argument registers it with the parent object -- so that when the parent gets deleted -- it'll delete its children (i.e. the label). So the normal Qt style is to not manually delete it, but reuse the label -- and when the containing widget is deleted, it'll automagically delete the children.

            Also, be very careful (i.e. don't do it) deleting shit (like the label) in events (like the key-press) cause other than the fact you can get weird problems (use deleteLater to get rid of those) -- but double deleting will probably cause your application to crash.

            @QImage img("1.png");
            QImage img1("1.png");
            ...@

            This is just creating a bunch of Objects on the "stack", they'll get deleted when the stack goes out of scope e.g. the next }

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sriky27
              wrote on last edited by
              #6

              Hi,

              Is there a tool to find memory leaks on symbian I gave a shot with HookLogger and I had a very long discussion in the Qt S60 interests mail chain about this. I did not get a convincing answer for this.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                danilocesar
                wrote on last edited by
                #7

                I've tried symbian apps (as hooklogger) for that, but I've gave up after some days of usage...
                Now, I'm using only valgrind and electricfence (and testing it under linux environment), and avoiding to use symbian's specific code.

                The result has been pretty satisfactory.

                <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
                Software Engineer

                1 Reply Last reply
                0
                • ? This user is from outside of this forum
                  ? This user is from outside of this forum
                  Guest
                  wrote on last edited by
                  #8

                  What would you recommend for Win32 environment? (Other than Purify)

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sriky27
                    wrote on last edited by
                    #9

                    But isn't it annoying to run the memory leaks tool in a different environment than Symbian to find leaks. There could cases where there could be a mix of Symbian and Qt code. What happens in that case. For that matter it applies to the other platforms also. How will this issue be addressed?

                    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