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
Forum Updated to NodeBB v4.3 + New Features

Memory leak using QThread issue

Scheduled Pinned Locked Moved Solved General and Desktop
35 Posts 5 Posters 14.4k Views 2 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.
  • SGaistS SGaist

    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.

    Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #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

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

      Cobra91151C 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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

        Cobra91151C 2 Replies Last reply
        2
        • A ambershark

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

          Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by Cobra91151
          #29

          @ambershark

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

          1 Reply Last reply
          0
          • SGaistS SGaist
            1. Remove the currently loaded translator
            2. Delete it
            3. Create the new translator
            4. Install it
            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by
            #30

            @SGaist

            OK. I will try it and reply.

            1 Reply Last reply
            0
            • Cobra91151C Offline
              Cobra91151C Offline
              Cobra91151
              wrote on last edited by Cobra91151
              #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
              • SGaistS SGaist
                1. Remove the currently loaded translator
                2. Delete it
                3. Create the new translator
                4. Install it
                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by Cobra91151
                #32

                @SGaist

                I have change appTranslator to stacked object, because I can't find the appropriate place to delete it. So now my function where I load local. files looks like this:

                Code:

                void Test::changeLanguage(int languageIndex)
                {
                    languageChanged = true;
                    QString systemLocale = QLocale::system().name();
                
                    switch (languageIndex) {
                        case 0:
                            if (systemLocale == "en_US") {
                                qApp->removeTranslator(&appTranslator);
                                appTranslator.load(":Localization/Test_en.qm");
                                qApp->installTranslator(&appTranslator);
                            }
                
                            if (systemLocale == "ru_RU") {
                                qApp->removeTranslator(&appTranslator);
                                appTranslator.load(":Localization/Test_ru.qm");
                                qApp->installTranslator(&appTranslator);
                            }
                
                            if (systemLocale == "uk_UA") {
                                qApp->removeTranslator(&appTranslator);
                                appTranslator.load(":Localization/Test_uk.qm");
                                qApp->installTranslator(&appTranslator);
                            }
                        break;
                
                        case 1:
                            qApp->removeTranslator(&appTranslator);
                            appTranslator.load(":Localization/Test_en.qm");
                            qApp->installTranslator(&appTranslator);
                        break;
                
                        case 2:
                            qApp->removeTranslator(&appTranslator);
                            appTranslator.load(":Localization/Test_ru.qm");
                            qApp->installTranslator(&appTranslator);
                        break;
                
                        case 3:
                            qApp->removeTranslator(&appTranslator);
                            appTranslator.load(":Localization/Test_uk.qm");
                            qApp->installTranslator(&appTranslator);
                        break;
                
                        default:
                            QMessageBox::critical(this, QObject::tr("Error"), QObject::tr("Can't change localization!"), QMessageBox::Ok);
                    }
                }
                

                I think this should work now?

                jsulmJ 1 Reply Last reply
                0
                • Cobra91151C Cobra91151

                  @SGaist

                  I have change appTranslator to stacked object, because I can't find the appropriate place to delete it. So now my function where I load local. files looks like this:

                  Code:

                  void Test::changeLanguage(int languageIndex)
                  {
                      languageChanged = true;
                      QString systemLocale = QLocale::system().name();
                  
                      switch (languageIndex) {
                          case 0:
                              if (systemLocale == "en_US") {
                                  qApp->removeTranslator(&appTranslator);
                                  appTranslator.load(":Localization/Test_en.qm");
                                  qApp->installTranslator(&appTranslator);
                              }
                  
                              if (systemLocale == "ru_RU") {
                                  qApp->removeTranslator(&appTranslator);
                                  appTranslator.load(":Localization/Test_ru.qm");
                                  qApp->installTranslator(&appTranslator);
                              }
                  
                              if (systemLocale == "uk_UA") {
                                  qApp->removeTranslator(&appTranslator);
                                  appTranslator.load(":Localization/Test_uk.qm");
                                  qApp->installTranslator(&appTranslator);
                              }
                          break;
                  
                          case 1:
                              qApp->removeTranslator(&appTranslator);
                              appTranslator.load(":Localization/Test_en.qm");
                              qApp->installTranslator(&appTranslator);
                          break;
                  
                          case 2:
                              qApp->removeTranslator(&appTranslator);
                              appTranslator.load(":Localization/Test_ru.qm");
                              qApp->installTranslator(&appTranslator);
                          break;
                  
                          case 3:
                              qApp->removeTranslator(&appTranslator);
                              appTranslator.load(":Localization/Test_uk.qm");
                              qApp->installTranslator(&appTranslator);
                          break;
                  
                          default:
                              QMessageBox::critical(this, QObject::tr("Error"), QObject::tr("Can't change localization!"), QMessageBox::Ok);
                      }
                  }
                  

                  I think this should work now?

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

                  @Cobra91151 It should work depending on where appTranslator is declared.
                  Back to what @SGaist suggested:

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

                  That's it.
                  Documentation actually says that removing translator does not delete it, see http://doc.qt.io/qt-5/qcoreapplication.html#removeTranslator
                  In your code you actually was removing the new translator instead of the old one:

                  appTranslator = new QTranslator(this); // You create new one
                  qApp->removeTranslator(appTranslator); // You try to remove the new one
                  appTranslator->load(":Localization/Test_ru.qm");
                   qApp->installTranslator(appTranslator);
                  

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

                  Cobra91151C 1 Reply Last reply
                  3
                  • jsulmJ jsulm

                    @Cobra91151 It should work depending on where appTranslator is declared.
                    Back to what @SGaist suggested:

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

                    That's it.
                    Documentation actually says that removing translator does not delete it, see http://doc.qt.io/qt-5/qcoreapplication.html#removeTranslator
                    In your code you actually was removing the new translator instead of the old one:

                    appTranslator = new QTranslator(this); // You create new one
                    qApp->removeTranslator(appTranslator); // You try to remove the new one
                    appTranslator->load(":Localization/Test_ru.qm");
                     qApp->installTranslator(appTranslator);
                    
                    Cobra91151C Offline
                    Cobra91151C Offline
                    Cobra91151
                    wrote on last edited by Cobra91151
                    #34

                    @jsulm

                    Thanks for code example. I have declared it in a header file as stacked object.

                    1 Reply Last reply
                    0
                    • Cobra91151C Offline
                      Cobra91151C Offline
                      Cobra91151
                      wrote on last edited by Cobra91151
                      #35

                      Issue is solved. Thanks all for the help.

                      1 Reply Last reply
                      1

                      • Login

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