Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instruments keep telling me I have memory leak

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 treeLeaked 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?

like image 467
StereoMatching Avatar asked Apr 24 '13 14:04

StereoMatching


People also ask

Why do I keep getting memory leaks?

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.


1 Answers

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.

like image 101
Blessy Nash Avatar answered Oct 05 '22 05:10

Blessy Nash