QT5 application crashed after running for some long time.....
-
You can run heob as a memory analyzer in Windows 10. It is discussed in the Qt docs. However, heob and valgrind find different things I have noticed. So, if possible, it might be valuable to run a virtual machine with Linux on it to test your code with valgrind. That assumes it can run in Linux though, so may not be applicable. Our apps are compile-able in both linux and windows. valgrind for instance, found alignment errors in unions due to compiler change from 32 bit to 64 bit. Not sure if heob could find that or not.
-
@VikramSamy said in QT5 application crashed after running for some long time.....:
how do set the debugger to break during the std:bad_alloc ??signal???
Usually you just start the program in the debugger and on an exception, the debugger jumps in.
As @fcarney suggested, you can try to run your app under another operating system, or on Windows, you can run it under MinGW instead MSVC, e.g.
Doing so usually gives you different compiler warnings / errors and sometimes different runtime behavior.
It's also worth to use the tools heob and valgrind, as @fcarney said. these are good in finding memory leaks.
-
I've experiencing same issue on Mac, I solved it by doing two things:
- put a breakpoint according to the exception, here std::bad_alloc::bad_alloc()
- add _GLIBCXX_DEBUG=1 to the Buid environment variables (unless the stack trace is meaningless)
Hope it helps.
-
@aha_1980 said in QT5 application crashed after running for some long time.....:
Usually you just start the program in the debugger and on an exception, the debugger jumps in.
Yup this what i expected, but the debugger didnt jump in.... so did i miss any kind of settings or configurations in the debugger??.
@mpergand said in QT5 application crashed after running for some long time.....:
I've experiencing same issue on Mac, I solved it by doing two things:
- put a breakpoint according to the exception, here std::bad_alloc::bad_alloc()
- add _GLIBCXX_DEBUG=1 to the Buid environment variables (unless the stack trace is meaningless)
Hope it helps.
HI @mpergand , what u mean by "put a breakpoint according to the exception, here std::bad_alloc::bad_alloc()", and where do i need to put the breakpoint, u mean i can tell in 'debugger configurations' to break the at the exception??? care for more details about what u saying??
-
@mpergand said in QT5 application crashed after running for some long time.....:
std::bad_alloc::bad_alloc()
Please read this page:
https://doc.qt.io/qtcreator/creator-debug-mode.html
You can have a test with std::out_of_range
In the breakpoint panel, right-clic->add breakpoint,
for type choose : C++ exception is thrown
for condition write: std::out_of_rangeIf you execute this code in debug mode:
std::vector<int> v; v.at(0);
The prg should stop and the stack trace panel shows the line where the exception occurs.
-
@fcarney said in QT5 application crashed after running for some long time.....:
valgrind for instance, found alignment errors in unions due to compiler change from 32 bit to 64 bit. Not sure if heob could find that or not.
No, heob can't do that.
It's mainly for heap related problems, like memory leaks and heap overflows. -
@fcarney said in QT5 application crashed after running for some long time.....:
You can run heob as a memory analyzer in Windows 10. It is discussed in the Qt docs. However, heob and valgrind find different things I have noticed. So, if possible, it might be valuable to run a virtual machine with Linux on it to test your code with valgrind. That assumes it can run in Linux though, so may not be applicable. Our apps are compile-able in both linux and windows. valgrind for instance, found alignment errors in unions due to compiler change from 32 bit to 64 bit. Not sure if heob could find that or not.
The issue: -the application memory increase once the application runs, in just small bytes but it continually increasing all the way until crash.
@fcarney i run heob and tried.... i just use the basic setting in the QT documentation related to heob, and i got more than 1650 issue, but anyway im only looking into any issue related to the line number of my code only, A example issues is as below and mostly the issues are referring the same function block, after my application crash i check the debug panel contained messages as below:-
the no 5,6 and 10 are actually code written by me, so i need to check this codes for some memory leak right??? so basically heob will just tell us which line of code is leaking memory and i need rewrite the function in some way that not leak memory, that should be the target, right??
So the reason for post is, how do we actually analyze heob data??? and use it to check the code??? because it show so many issue 1650, and so many lines of code, i think i just need ignored the other line of codes not written by me and just check on all the lines of codes belong to me,and mostly its the same function block again, below is one of the example section of code:-
//your code here void SUB_EVENT_PANEL::Change_Chain_Track_Status_Eventviewer(LED_STATUS led_status) { STATUS_BIT_TYPE status_mask; STATUS_BIT_TYPE i; // i need be static,???no need status_mask =1; //0x01 //initialize the status messages //m_leak TableMessages<<tr("HEALTHY")<<("ATS ALARMED!!")<<tr("SYSTEM HEALTHY")<<tr("IN BYPASS MODE")<<tr("LCP/PCP TAMPERED")<<tr("INTRUSION TRIGGERED")<<tr("CABLE FAULT")<<tr("FUTURE NOT USED...")<<tr("ACKNOWLEDGE")<<tr("RESET...")<<tr("A ON...")<<tr("B ON...")<<tr("PLC COMM OK"); TableMessagesNOT<<tr("ATS ALARMED!!")<<tr("ATS HEALTHY!!")<<("SYSTEM UNHEALTHY")<<tr("IN NORMAL MODE")<<tr("LCP/PCP SECURE")<<tr("INTRUSION CLEARED")<<tr("CABLE OK")<<tr("FUTURE NOT USED...")<<tr("N/A")<<tr("N/A")<<tr("A OFF...")<<tr("B OFF...")<<tr("PLC COMM LOSS"); ........ continues other code }
so one of issue the heob points to this TableMessages which i declared at the header as below:
//your code here private: QStringList TableMessages; QStringList TableMessagesNOT;
and the above function is called everytime a tcpip packet is received and parsed, a packet is send every 0.02 by a PLC client, and by using a for loop i check if there any event (any bit is toggled) and if yes write to the HMI table as below, part of the coding:-
//your code here case 2:qDebug() <<"caseN_E 2-system healthy"; if (Data_Process::Get_Status_Bit(led_status.button_statusR, System_Health_STATUS_BIT)) { TableWrite(i); } if (!(Data_Process::Get_Status_Bit(led_status.button_statusR, System_Health_STATUS_BIT))) { TableWriteNOT(i); } break;
so according to heob most of my data leak are coming from this function,so the memory allocated for this QStringList TableMessages; is the problem as heob point to this exact line, and few others.... is this because i allocated the memory without using NEW keyword??? any ideas,tips,hints??
-
@VikramSamy Do you remove values from TableMessages and TableMessagesNOT somewhere?
-
@VikramSamy said in QT5 application crashed after running for some long time.....:
TableMessages<<tr("HEALTHY")<<("ATS ALARMED!!")<<tr("SYSTEM HEALTHY")<<tr("IN BYPASS
MODE")<<tr("LCP/PCP TAMPERED")<<tr("INTRUSION TRIGGERED")<<tr("CABLE FAULT")<<tr("FUTURE NOT USED...")<<tr("ACKNOWLEDGE")<<tr("RESET...")<<tr("A ON...")<<tr("B ON...")<<tr("PLC COMM OK");TableMessagesNOT<<tr("ATS ALARMED!!")<<tr("ATS HEALTHY!!")<<("SYSTEM UNHEALTHY")<<tr("IN NORMAL MODE")<<tr("LCP/PCP SECURE")<<tr("INTRUSION CLEARED")<<tr("CABLE OK")<<tr("FUTURE NOT USED...")<<tr("N/A")<<tr("N/A")<<tr("A OFF...")<<tr("B OFF...")<<tr("PLC COMM LOSS");
If you are adding to these lists and never removing anything, then yes you are consuming more and more memory every time this function is called. If you need to rebuild these lists on each run, then clear them out before you add more to them.
-
Hi yup i dint remove the QStringList anywhere, as i thought it will be automatically destroyed when it gets out of the function, what i didn't realize is that i created the QStringlist at the headers file and initialized it in the functions ,as per that it will allocated in the heap/dynamically and will be continuously allocated new memory each time the function is called..ok i got it, ....
now what the best thing to do, how do i remove it?? is it better to remained in this way, allocated in the heap memory and removing it when not used?? so i need use the removeall or clearall to deallocated memory?? care for a correct example of deleting the Qstringlist properly??
Anyway after some reading,i just modify my code to allocated the QstringList inside the functions itself (like local variable),
so now memory is allocated in the stack and it will be destroyed when it exit the functions (out of scope) and i tested the code and the memory does not increase as previously ,so it is better now .the new code is as below:
void SUB_EVENT_PANEL::Change_Chain_Track_Status_Eventviewer(LED_STATUS led_status) { QStringList TableMessagesNOT; // now i declared here TableMessagesNOT<<tr("ALARMED!!") // and initialize here } //so will be deleted after go out of scope, and memory released.
so my question which is the better, use as my original code and delete the Qstringlist somewhere, or create the QStringlist in the stack and let it get destroyed by itself after getting out of scope as above ??
-
@VikramSamy said in QT5 application crashed after running for some long time.....:
so my question which is the better, use as my original code and delete the Qstringlist somewhere, or create the QStringlist in the stack and let it get destroyed by itself after getting out of scope as above ??
There is no generic answer to this question as it simply depends on the use case. If you only need this list in that method then you simply allocate it as local variable. Having it as member variable in the class is only needed if you need the list at several places in your class.
By the way this is not heap allocation:private: QStringList TableMessages; QStringList TableMessagesNOT;