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. How to minimize memory leak in ??

How to minimize memory leak in ??

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 3.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.
  • A Offline
    A Offline
    Anny
    wrote on last edited by Anny
    #1

    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

    aha_1980A 1 Reply Last reply
    0
    • A Anny

      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

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Qt has to stay free or it will die.

      A 1 Reply Last reply
      1
      • aha_1980A aha_1980

        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

        A Offline
        A Offline
        Anny
        wrote on last edited by Anny
        #3

        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?

        jsulmJ 1 Reply Last reply
        0
        • A Anny

          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?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            @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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            aha_1980A 1 Reply Last reply
            2
            • VRoninV VRonin

              @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

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by aha_1980
              #6

              @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 has to stay free or it will die.

              1 Reply Last reply
              1
              • kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                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.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                2
                • A Offline
                  A Offline
                  Anny
                  wrote on last edited by Anny
                  #8

                  @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

                  A 1 Reply Last reply
                  0
                  • A Anny

                    @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

                    A Offline
                    A Offline
                    Anny
                    wrote on last edited by Anny
                    #9

                    @jsulm I somehow managed to solve those valgrind tool's error and reduced memory leak but didnt get rid of it.

                    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