How to minimize memory leak in ??



  • System :Qt 5.5.1 (GCC 5.2.1 20151129, 32 bit) I have made qt qml desktop application with multi-threading using QThread like this :

    QThread *thrd1 = new QThread;
    QThread *thrd2 = new QThread;
    QThread *thrd3 = new QThread;
    QThread *thrd4 = new QThread;

    myclass1 *class1 = new myclass1();
    myclass2 *class2 = new myclass2();
    myclass3 *class3 = new myclass3();
    myclass4 *class4 = new myclass4();

    class1->moveToThread(thrd1);
    thrd1->start(QThread::HighestPriority)
    class2->moveToThread(thrd2);
    thrd2->start(QThread::NormalPriority)
    class3->moveToThread(thrd3);
    thrd3->start(QThread::LowPriority)
    class4->moveToThread(thrd4);
    thrd4->start(QThread::LowestPriority)

    I have used QTimer based slots in classes, so that data can be updated from c++ to qml on every 100ms and QThread can run untill application is closed.
    This application is working fine but it has memory leakage.

    I have created many local variables(without pointers) in slots of classes and also used many variables declared in header file of purticular class as well as global variables declared in global header file.

    I also have myclass::~myclass() destructor in object class .cpp file.

    How can I minimize memory leak? What is a correct way to call class and connect it QThread ? Do I need to delete the object?

    Regards



  • Hi @Anny,

    all the code you showed is fine and will not leak. the memory is freed when your program exits.

    however, if you allocate memory (e.g. in your threads) again and again, your memory usage grows. if that memory is not freed from your side when its no longer used, than your program leaks.

    Regards



  • Hi @aha_1980 , you are right I am allocating memory in threads again and again but those variables are local and as far as I know local variables allocate memory in stack so that memory is freed automatically. Am I correct ? Do I need to delete that memory too? if yes how to do that?


  • Moderators

    @Anny Local variables are not a problem - stack allocated memory is automatically freed when it goes out of scope. You need to check all allocations with "new" and free that memory when not needed anymore. You can use Valgrind tool.
    You question is way to generic. You should not minimize memory leaks you should get rid of them completely. If you allocate memory dynamically free it when not used.



  • @VRonin said in Interfaces, abstract, concrete classes and QObject:

    This is the flowchart I normally follow, it covers 99% of the cases:

    • is it a QObject?
      • yes
        • can you give it a parent? (for example moveToThread() prevents the parent-child relationship)
          • yes: use a normal pointer and set the parent
          • no: connect the deleteLater() slot to some signal
      • no
        • can multiple places share ownership of the object or pass ownership from one another? (e.g. in containes)
          • yes: use std::shared_ptr
          • no: use std::unique_ptr

    If you need to use the pointer in a function that should not manage ownership but just access (read/write) the object (this covers 90% of use cases) then use a raw pointer argument (use std::unique_ptr::get() or std::shared_ptr::get() to retrieve the [non-owning] raw pointer from smart pointers)

    @aha_1980 said in How to minimize memory leak in ??:

    all the code you showed is fine and will not leak

    I'm not sure I agree. here both the threads and the myclass# objects are leaked

    I also have myclass::~myclass() destructor in object class .cpp file.

    One more place where memory might be leaked is forgetting to declare the destructor of base classes as virtual



  • @VRonin said in How to minimize memory leak in ??:

    @aha_1980 said in How to minimize memory leak in ??:

    all the code you showed is fine and will not leak

    I'm not sure I agree. here both the threads and the myclass# objects are leaked

    Of course the ways you showed are better suited, but what I meant was, if these objects are created once and live as long as the program is running, this will not lead to increasing memory during runtime.

    but you are right, allocated memory has to be freed when no longer needed. you showed some ways in your reply.


  • Qt Champions 2016

    If this is the entirety of the code (dealing with setting up the threading), then all the objects are leaking, which includes the thread objects and the workers. Every new goes along with a delete unless otherwise agreed upon (like in the case of Qt's parent-child relationship). If you don't trust your code to not throw exceptions and/or to free the memory explicitly then use stack based objects to manage the lifetime for you - that is what @VRonin wrote - std::unique_ptr or QScopedPointer. My advice is not to mix QSharedPointer/std::shared_ptr with QObject derived classes unless you love debugging through deep and cryptic stack traces.

    Of course if you don't want to use superfluous news, like in this case, you can just allocate everything with auto-storage and leave the compiler to do the heavy lifting. That's what I do if I can.

    @VRonin said in How to minimize memory leak in ??:

    One more place where memory might be leaked is forgetting to declare the destructor of base classes as virtual

    Only when you're deleting through a base class pointer, but it's moot for QObject derived classes as the destructor of QObject is virtual already. Also the compiler emits a warning in this case. It's a good idea to compile with -Werror in release so all warnings are treated as errors and you ship a clean code in the end.



  • @jsulm I checked in all threads,nothing is allocating memory with 'new' in any thread. I am calling other class and its function in threads using simple method like ( eg. testclass test) i.e without 'new'. And I also tried valgrind tool it gives me following errors and warning before starting the app , "WARNING: serious error when reading debug info" "use of uninitialized value of size 4" "invalid write of size 4 "
    @aha_1980 , @VRonin I tried the method you suggested. As I want my threads to live as long as application I could not put deleteLater() slot in any running case so I called it before closing the app. It didn't work.
    @kshegunov I am not much familiar with smart pointer can you provide some example code or links ? I got it some but those are just describing it , I require some example
    Thank you


Log in to reply
 

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