15

I am new to mac and instruments, I use it to test the my Qt app, I found a lot of leaked objects, almost all of them are coming from Qt lib.I check my codes very careful but can't find the problem. To avoid the problem of memory leak, I strictly obey the rules of RAII, always let class handle the resources, make sure every widget has a parent, those widget without parent(intented) will guard by smart pointer or Qt::WA_DeleteOnClose.

To fix the memory leak warning, I write a very simple Qt app and use it as a test, the instruments always show that I have some memory leaks(as graph) even the most simplest Qt app I created.

    #include <QApplication>
    #include <QLabel>

        int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);


        QLabel w;
        w.resize(320, 240);
        w.show();

        return a.exec();
    }

The graph of instruments

Call tree

Leaked object

I alter the codes a little bit, and see the memory leak show by Instruments would keep rising or not.

#include <QApplication>
    #include <QLabel>

        int main(int argc, char *argv[])
       {
        QApplication a(argc, argv);


        for(size_t i = 0; i != 100; ++i){
        QLabel w;
        w.resize(320, 240);
        w.show();
       }
       QLabel w;
       w.resize(320, 240);
       w.show();

        return a.exec();
    }

Call tree Leaked object

The memory leaking do increase, I strongly hope that this is a mistake of the instrument, else I have to drop back to Qt4(and I don't know it would have the same problem or not).I don't think this simple app could pass the quality check of the mac app store(OSX). What is going on?How should I explain this phenomenon?If there are no memory leak, I should not see any message of the leak object, am I correct?A bug of Qt5.0.2?

Noel
  • 10,152
  • 30
  • 45
  • 67
StereoMatching
  • 4,971
  • 6
  • 38
  • 70
  • 1
    If you just leave your program running for a very long time, does the memory working set increase steadily? I wouldn't be hasty to assume there's something wrong with the leak detector, but this wouldn't be the first time I've seen one give false positives. – Timothy Shields Apr 24 '13 at 14:36
  • I edit my answer, give it a new try, and the memory leak shown by instruments do increase, what a bad news – StereoMatching Apr 24 '13 at 15:13
  • What I meant by "does the memory working set increase steadily?" was "If you watch the Windows Task Manager window (or the Mac equivalent), does the working set column increase steadily?" – Timothy Shields Apr 24 '13 at 15:16
  • I change it to infinite loop, the memory do increase(would increase and decrease, but in the long run, it is increasing), no matter private memory or real memory. – StereoMatching Apr 24 '13 at 15:35
  • Ignoring Instruments, if you view your app in Activity Monitor, does the Real Mem keep increasing? – TheDarkKnight Apr 26 '13 at 08:33
  • even in Qt 4 valgrind show me memory leaks during the initialization of the Qapplication object. + False positive do happen... – UmNyobe Apr 26 '13 at 12:33
  • @UmNyobe: If _valgrind_ gives memory leaks, than it is worth investigating, because valgrind only reports memory to which it can't find anything that could be pointer to it. I don't know whether the mac instrument is that good though and wouldn't bet on it. – Jan Hudec Apr 26 '13 at 12:42
  • @Merlin069, yes, the real memory in activity monitor keep increasing, if you change the loop to while(1), it is very obvious to see that phenomenon. – StereoMatching Apr 26 '13 at 14:07
  • 2
    @UmNyobe, someone reported the memory leak problem detected by valgrind, but they do not have a will to fix it, they said "These are small one-time "leaks" and therefore irrelevant." https://bugreports.qt-project.org/browse/QTBUG-7505 – StereoMatching Apr 26 '13 at 14:07
  • 2
    I reported this as a bug yesterday. https://bugreports.qt-project.org/browse/QTBUG-30864 If you do not frequently create and delete those QWidget, the real memory and private memory would not keep raising, which are the cases of most apps.But what if some apps do need to create and delete those widgets? – StereoMatching Apr 26 '13 at 14:15
  • Thanks for the link to the bug report. – UmNyobe Apr 26 '13 at 15:14
  • Could it be that resizing and showing the label is creating lots of Cocoa objects behind the scenes? If so, perhaps you need to create an autoreleasepool? for(size_t i = 0; i != 100; ++i){ @autoreleasepool { QLabel w; w.resize(320, 240); w.show(); } } – Lyndsey Ferguson Apr 26 '13 at 21:22
  • Regarding @StereoMatching's [bug report](https://bugreports.qt-project.org/browse/QTBUG-30864): Please do also vote for this bug in order to give it more importance. I find this bug very annoying, too, because it makes it hard to find own memory leaks. – Jonny Dee May 01 '13 at 07:22
  • I wouldn't bet my house on memory leak utilities. I know VS leak detector gives false positive results in certain cases when memory allocated behind the scenes (i.e. not by you) is miscategorized as memory that should be handled by you. Supposedly it isn't actually leaked though. – ChiefTwoPencils May 05 '13 at 07:59
  • To be honest Qt contains quite a bunch of memory leaks, although they indeed are very insignificant, usually just few kb of memory overhead, even in very long run time. – Petr Mar 21 '17 at 12:44

1 Answers1

2

Memory is indeed being leaked, which a trusted framework should never do.

Basically, the framework should use smart pointers, or offending memory leaks should be traced and dealt with "manually".

If there are was no leak then all memory would be returned to the heap after use.

According to bugreports.qt-project.org/browse/QTBUG-7505 this bug is in version 4.6.0.