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. Memory leak using QThread issue
QtWS25 Last Chance

Memory leak using QThread issue

Scheduled Pinned Locked Moved Solved General and Desktop
35 Posts 5 Posters 13.5k Views
  • 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.
  • C Cobra91151
    4 Apr 2017, 08:36

    @J.Hilk

    I have changed code to:

     appCheckUpdate = new CheckUpdates();
     appCheckUpdatesThread = new QThread();
     appCheckUpdate->moveToThread(appCheckUpdatesThread);
     connect(appCheckUpdatesThread, &QThread::started, appCheckUpdate, &CheckUpdates::checkUpdate);
     connect(appCheckUpdate, &CheckUpdates::updateData, this, &Changelog::getUpdateData);
     connect(appCheckUpdate, &CheckUpdates::networkError, this, &Changelog::changeLogUpdateError);
     connect(appCheckUpdate, &CheckUpdates::localeError, this, &Changelog::updateLocaleError);
     connect(appCheckUpdate, &CheckUpdates::finished, appCheckUpdatesThread, &QThread::quit);
     connect(appCheckUpdatesThread, &CheckUpdates::destroyed, appCheckUpdatesThread, &QThread::deleteLater);
     appCheckUpdatesThread->start();
    

    But the problem still exists, it leaks memory. So this - connect(appCheckUpdatesThread, &CheckUpdates::destroyed, appCheckUpdatesThread, &QThread::deleteLater); - deletes only appCheckUpdatesThread (QThread) object.

    So should I also delete appCheckUpdate by calling delete appCheckUpdate?

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 4 Apr 2017, 10:48 last edited by
    #12

    @Cobra91151 It should be:

    connect(appCheckUpdatesThread, &QThread::finished, appCheckUpdatesThread, &QThread::deleteLater);
    

    In http://doc.qt.io/qt-5.8/qthread.html#finished
    "This signal can be connected to QObject::deleteLater(), to free objects in that thread."

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

    1 Reply Last reply
    2
    • J J.Hilk
      4 Apr 2017, 08:38

      @Cobra91151
      yes of course,
      at the end of your threaded operation, you'll have to delete your QThread object and your Worker object, seperately.

      In case you haven't seen it, heres an wiki-example

      C Offline
      C Offline
      Cobra91151
      wrote on 4 Apr 2017, 12:07 last edited by
      #13

      @J.Hilk

      Thank you. I have fixed the memory leak issue with changelog, but I also have the memory leak when I reload some WMI data during language change of my app. I will try to fix it and reply.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Cobra91151
        wrote on 8 Apr 2017, 13:58 last edited by Cobra91151 4 Aug 2017, 14:03
        #14

        Hi! I have some WMI memory leak issue.

        Code:

        //Initialization
        IWbemLocator *pLocator = 0;
        IWbemServices *pService = 0;
        IEnumWbemClassObject* pEnumerator = NULL;
        IWbemClassObject *pclsObj = NULL;
        
        while (pEnumerator)
        {
             hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
        
              VARIANT processName;
              pclsObj->Get(L"Name", 0, &processName, 0, 0);
              QString userProcessName;
              userProcessName = QString::fromWCharArray(processName.bstrVal);
               
              emit testData(userProcessName);
              VariantClear(&processName);
        }
        
        //Cleanup
         pService->Release();
         pLocator->Release();
         //pEnumerator->Release(); - Clang Static Analyzer displays issue - called C++ object pointer is null
         //pclsObj->Release(); - Clang Static Analyzer displays issue - called C++ object pointer is null
        

        Every time I change language in my app it takes 1 - 5 MB RAM. How to fix this issue? Thanks in advance.

        A 1 Reply Last reply 8 Apr 2017, 17:59
        0
        • C Cobra91151
          8 Apr 2017, 13:58

          Hi! I have some WMI memory leak issue.

          Code:

          //Initialization
          IWbemLocator *pLocator = 0;
          IWbemServices *pService = 0;
          IEnumWbemClassObject* pEnumerator = NULL;
          IWbemClassObject *pclsObj = NULL;
          
          while (pEnumerator)
          {
               hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
          
                VARIANT processName;
                pclsObj->Get(L"Name", 0, &processName, 0, 0);
                QString userProcessName;
                userProcessName = QString::fromWCharArray(processName.bstrVal);
                 
                emit testData(userProcessName);
                VariantClear(&processName);
          }
          
          //Cleanup
           pService->Release();
           pLocator->Release();
           //pEnumerator->Release(); - Clang Static Analyzer displays issue - called C++ object pointer is null
           //pclsObj->Release(); - Clang Static Analyzer displays issue - called C++ object pointer is null
          

          Every time I change language in my app it takes 1 - 5 MB RAM. How to fix this issue? Thanks in advance.

          A Offline
          A Offline
          ambershark
          wrote on 8 Apr 2017, 17:59 last edited by
          #15

          @Cobra91151 This isn't Qt code.. You should probably post this question on a win32 API message board.

          Also are you sure it's actually leaking? Just because the size of memory for your running application grows does not mean it's leaking. You need something like valgrind to test for leaks on exit.

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          C A 2 Replies Last reply 8 Apr 2017, 20:01
          1
          • A ambershark
            8 Apr 2017, 17:59

            @Cobra91151 This isn't Qt code.. You should probably post this question on a win32 API message board.

            Also are you sure it's actually leaking? Just because the size of memory for your running application grows does not mean it's leaking. You need something like valgrind to test for leaks on exit.

            C Offline
            C Offline
            Cobra91151
            wrote on 8 Apr 2017, 20:01 last edited by Cobra91151 4 Aug 2017, 20:03
            #16

            @ambershark

            I have checked it in Task Manager. For example, my app takes 25 MB RAM, when changing languages it grows to 30 MB and 35, 40... and never release it. So should I consider this as not a leak?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 8 Apr 2017, 20:22 last edited by
              #17

              What do you mean by changing languages ? What do you do to make that happen ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              C 1 Reply Last reply 8 Apr 2017, 20:44
              0
              • S SGaist
                8 Apr 2017, 20:22

                What do you mean by changing languages ? What do you do to make that happen ?

                C Offline
                C Offline
                Cobra91151
                wrote on 8 Apr 2017, 20:44 last edited by Cobra91151 4 Aug 2017, 20:50
                #18

                @SGaist

                I have all WMI data in QTreeWidgetItems (QTreeWidget). I have two columns:
                1 - Property
                2 - Data

                Property and data I get from a worker class which connected with signals and slots to thread. So to change property to another language I call clear function on QTreeWidget to delete all property and data, then I call function which connects worker to thread and get this data as QTreeWidgetItems in chosen (translated) language.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 8 Apr 2017, 20:56 last edited by
                  #19

                  Are you loading any language file in order to provide translations ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  C 1 Reply Last reply 8 Apr 2017, 21:10
                  0
                  • S SGaist
                    8 Apr 2017, 20:56

                    Are you loading any language file in order to provide translations ?

                    C Offline
                    C Offline
                    Cobra91151
                    wrote on 8 Apr 2017, 21:10 last edited by Cobra91151 4 Aug 2017, 21:13
                    #20

                    @SGaist

                    Yes, but when QComboBox index changes I call this function to load appropriate translation file:

                    Some code:

                    appTranslator = new QTranslator(this);
                    qApp->removeTranslator(appTranslator);
                    appTranslator->load(":Localization/Test_ru.qm");
                     qApp->installTranslator(appTranslator);
                    

                    and when apply button is clicked I retranslate string and reload WMI data using my loadTraslatedText function

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 8 Apr 2017, 21:17 last edited by
                      #21

                      Your code has one tiny problem: you are removing the translator you just created and then you add it again after loading the translation file, so basically you never remove anything and just keep adding new ones.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      C 1 Reply Last reply 8 Apr 2017, 21:30
                      0
                      • S SGaist
                        8 Apr 2017, 21:17

                        Your code has one tiny problem: you are removing the translator you just created and then you add it again after loading the translation file, so basically you never remove anything and just keep adding new ones.

                        C Offline
                        C Offline
                        Cobra91151
                        wrote on 8 Apr 2017, 21:30 last edited by Cobra91151 4 Aug 2017, 21:31
                        #22

                        @SGaist

                        I have changed code:

                        appTranslator = new QTranslator(this);
                        appTranslator->load(":Localization/Test_ru.qm");
                         qApp->installTranslator(appTranslator);
                        

                        But the leak is still exists.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 8 Apr 2017, 21:31 last edited by
                          #23

                          Isn't it the same code as before ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          C 1 Reply Last reply 8 Apr 2017, 21:32
                          1
                          • S SGaist
                            8 Apr 2017, 21:31

                            Isn't it the same code as before ?

                            C Offline
                            C Offline
                            Cobra91151
                            wrote on 8 Apr 2017, 21:32 last edited by Cobra91151 4 Aug 2017, 21:44
                            #24

                            @SGaist

                            Check again. I have copied the wrong one.

                            I don't think that the problem is with language change function itself, because when I have commented the getWMIData function no leak is found.

                            So the problem is with actual getWMIData which contains other functions such as osData, cpuData.... which lead to worker class which has about 10 thousand lines of code.

                            I think the only way to fix this, is to figure out how to change all properties to appropriate language without reloading their data.

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 8 Apr 2017, 22:14 last edited by
                              #25

                              You are still still adding one new translator each time you change the language, start by fixing that. Remove the one currently installed and then load the new one.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              C 1 Reply Last reply 8 Apr 2017, 22:29
                              0
                              • S SGaist
                                8 Apr 2017, 22:14

                                You are still still adding one new translator each time you change the language, start by fixing that. Remove the one currently installed and then load the new one.

                                C Offline
                                C Offline
                                Cobra91151
                                wrote on 8 Apr 2017, 22:29 last edited by Cobra91151 4 Aug 2017, 22:30
                                #26

                                @SGaist

                                So I put initialization of translator (appTranslator = new QTranslator(this);) in the constructor.

                                appTranslator->load(":Localization/Test_ru.qm");
                                qApp->installTranslator(appTranslator);
                                

                                Then I have added qApp->removeTranslator(appTranslator); at the end to loadTraslatedText function, but now my other windows are not translated (when I open them). The problem is with qApp->removeTranslator(appTranslator); So where to remove the one currently installed?

                                1 Reply Last reply
                                0
                                • A ambershark
                                  8 Apr 2017, 17:59

                                  @Cobra91151 This isn't Qt code.. You should probably post this question on a win32 API message board.

                                  Also are you sure it's actually leaking? Just because the size of memory for your running application grows does not mean it's leaking. You need something like valgrind to test for leaks on exit.

                                  A Offline
                                  A Offline
                                  ambershark
                                  wrote on 8 Apr 2017, 22:33 last edited by
                                  #27

                                  @ambershark said in Memory leak using QThread issue:

                                  @Cobra91151 This isn't Qt code.. You should probably post this question on a win32 API message board.

                                  Also are you sure it's actually leaking? Just because the size of memory for your running application grows does not mean it's leaking. You need something like valgrind to test for leaks on exit.

                                  Yea checking active running memory does not show you leaks. If it changes in task manager that is ok, if when you exit the app there are still pointers to memory that didn't get cleaned up then those are leaks.

                                  If as you watch your program run and be used it is constantly growing while running in the task manager that can be a leak too, but not just because you did something like allocate a new language.

                                  The way memory usually works is block are allocated and freed as they are needed. So if you allocate a new block, even though you delete your old one, the OS doesn't necessarily reclaim that memory unless it is needed. So in task manager you would see it change unless you were out of memory then you would see it reclaimed.

                                  So unless you see it constantly growing and growing, out of control, while running it's probably not leaking. That is why you need a tool like valgrind to detect actual leaks.

                                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                                  C 1 Reply Last reply 9 Apr 2017, 09:14
                                  0
                                  • S Offline
                                    S Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 8 Apr 2017, 23:19 last edited by
                                    #28
                                    1. Remove the currently loaded translator
                                    2. Delete it
                                    3. Create the new translator
                                    4. Install it

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    C 2 Replies Last reply 9 Apr 2017, 09:15
                                    2
                                    • A ambershark
                                      8 Apr 2017, 22:33

                                      @ambershark said in Memory leak using QThread issue:

                                      @Cobra91151 This isn't Qt code.. You should probably post this question on a win32 API message board.

                                      Also are you sure it's actually leaking? Just because the size of memory for your running application grows does not mean it's leaking. You need something like valgrind to test for leaks on exit.

                                      Yea checking active running memory does not show you leaks. If it changes in task manager that is ok, if when you exit the app there are still pointers to memory that didn't get cleaned up then those are leaks.

                                      If as you watch your program run and be used it is constantly growing while running in the task manager that can be a leak too, but not just because you did something like allocate a new language.

                                      The way memory usually works is block are allocated and freed as they are needed. So if you allocate a new block, even though you delete your old one, the OS doesn't necessarily reclaim that memory unless it is needed. So in task manager you would see it change unless you were out of memory then you would see it reclaimed.

                                      So unless you see it constantly growing and growing, out of control, while running it's probably not leaking. That is why you need a tool like valgrind to detect actual leaks.

                                      C Offline
                                      C Offline
                                      Cobra91151
                                      wrote on 9 Apr 2017, 09:14 last edited by Cobra91151 4 Sept 2017, 11:34
                                      #29

                                      @ambershark

                                      Thanks, but the problem is with WMI. And I'm working to fix it.

                                      1 Reply Last reply
                                      0
                                      • S SGaist
                                        8 Apr 2017, 23:19
                                        1. Remove the currently loaded translator
                                        2. Delete it
                                        3. Create the new translator
                                        4. Install it
                                        C Offline
                                        C Offline
                                        Cobra91151
                                        wrote on 9 Apr 2017, 09:15 last edited by
                                        #30

                                        @SGaist

                                        OK. I will try it and reply.

                                        1 Reply Last reply
                                        0
                                        • C Offline
                                          C Offline
                                          Cobra91151
                                          wrote on 9 Apr 2017, 11:15 last edited by Cobra91151 4 Sept 2017, 11:16
                                          #31

                                          Thank you. I have fixed the WMI memory leak issue by releasing resources.

                                          Code:

                                                      VariantClear(&serviceName);
                                                      VariantClear(&servicePath);
                                                      VariantClear(&serviceID);
                                                      VariantClear(&serviceType);
                                                      VariantClear(&serviceState);
                                                      VariantClear(&serviceStatus);
                                                      VariantClear(&serviceErrorControl);
                                                      VariantClear(&serviceStartMode);
                                                      VariantClear(&serviceWaitHint);
                                                      VariantClear(&serviceExitCode);
                                                      pclsObj->Release();
                                                  }
                                              }
                                          
                                              // Cleanup
                                              pService->Release();
                                              pLocator->Release();
                                              pEnumerator->Release();
                                              CoUninitialize();
                                              emit finished();
                                              return 0;   // Program successfully completed.
                                          }
                                          
                                          1 Reply Last reply
                                          0

                                          21/35

                                          8 Apr 2017, 21:17

                                          • Login

                                          • Login or register to search.
                                          21 out of 35
                                          • First post
                                            21/35
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved