Memory leak using QThread issue
-
@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." -
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.
-
@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.
-
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?
-
What do you mean by changing languages ? What do you do to make that happen ?
-
I have all
WMI
data inQTreeWidgetItems
(QTreeWidget
). I have two columns:
1 - Property
2 - DataProperty and data I get from a
worker
class which connected with signals and slots to thread. So to change property to another language I callclear
function onQTreeWidget
to delete all property and data, then I call function which connects worker to thread and get this data asQTreeWidgetItems
in chosen (translated) language. -
Are you loading any language file in order to provide translations ?
-
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 myloadTraslatedText
function -
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.
-
Isn't it the same code as before ?
-
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 asosData, cpuData....
which lead toworker
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.
-
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.
-
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 toloadTraslatedText
function, but now my other windows are not translated (when I open them). The problem is withqApp->removeTranslator(appTranslator);
So where to remove the one currently installed? -
@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.
-
- Remove the currently loaded translator
- Delete it
- Create the new translator
- Install it
-
Thanks, but the problem is with
WMI
. And I'm working to fix it.