Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

QT5 application crashed after running for some long time.....



  • My QT5 applications crashes after running for sometime, for example after 1 day or less....within that 1 day everything works fine and as accordingly,then the application crashes. The development environment was using windows 10.

    if i run the application in windows 7, it crashes and saying:-

    exception code:- 400000015, fault module name: libstdc++-6.dll

    0_1560923974507_window7_QT5crash.png

    above the screenshot from window 7.

    And then i try to run in windows 10, here it just crashes and terminates, no any messages.

    Finally then i run the applications in my QT5 development environment in debug mode, i get this:-

    terminate called after throwing an instance of std bad_alloc
    what(): syd::bad_alloc
    terminate call recursively

    0_1560924144660_windows10crash_debugmode.png

    above is screenshot from windows 10-QT5 debug mode.

    how to locate the problem as there is many lines of code here, any hints, tips and tricks to analysis this problem?? usually this message is related to "out-memory" as what i read from googling here&there, this means a code is using some memory until it finish,that why it working correctly at beginning and crashes when memory finish, is it so?? any ideas how start debugging this issue?? any tips, any plugin available in which i can install and find the memory bug??


  • Qt Champions 2018

    @VikramSamy said in QT5 application crashed after running for soem long time.....:

    how to locate the problem as there is many lines of code here

    Check the stack trace - what is the last line of your code there?
    Did you check the memory consumption of your app? bad_alloc could mean you're out of memory.


  • Moderators

    Hi @VikramSamy

    to your first issue, your Win7 system seems to be missing some dlls to run, I would suggest some program like dependecywalker to check and ship the missing files.

    Your 2nd issue:
    Jep seems like you've been a naught boy and not cleaned up after your self ;-)
    Sadly, finding the memory leak could very well be difficult.

    Mabye this will help ?



  • @jsulm said in QT5 application crashed after running for some long time.....:

    @VikramSamy said in QT5 application crashed after running for soem long time.....:

    how to locate the problem as there is many lines of code here

    Check the stack trace - what is the last line of your code there?
    Did you check the memory consumption of your app? bad_alloc could mean you're out of memory.

    Hi how to check the stack trace....where can i find the options??? is it i need to install some plugins?? im using window 10 as the development enviroment, i only saw the options to use valgrind but need linux... can u point to any tutorial on the stack tracing in QT5?? how about the QML profiler, is the profiler this for QT quick applications only?, because most the tutorial is based on QT quick applications...


  • Qt Champions 2018

    @VikramSamy

    Hi how to check the stack trace....where can i find the options???

    Run your program from your favorite debugger. After the crash, you can inspect the stacktrace. sometimes its called backtrace or call stack, its all the same.

    In Creator, its in the lower debug area next to breakpoints.



  • @jsulm said in QT5 application crashed after running for some long time.....:

    Check the stack trace - what is the last line of your code there?
    Did you check the memory consumption of your app? bad_alloc could mean you're out of memory.

    @aha_1980 said in QT5 application crashed after running for some long time.....:

    Run your program from your favorite debugger. After the crash, you can inspect the stacktrace. sometimes its called backtrace or call stack, its all the same.
    In Creator, its in the lower debug area next to breakpoints.

    Hi @jsulm what i notice in the window 10 task manager, the applications memory usage is increasing as time goes on and finally crashes....

    as @aha_1980 suggested i run in the debugger mode, but i cant see any debug messages or trace message on the debug windows, so how to set the debugger to catch the std::bad_alloc exception/signal and also show the debug messages?? where do i need to set the breakpoints???

    0_1561364549515_windows10crash_debugmode_qt5_cut.png
    this above screenshot was taken when the application is running in debugger mode but i didnt set any breakpoints...
    as u can see in the attached screenshot, it just crashed and gives the std:bad_alloc messages in the application output but other debug windows are just empty, no any messages got displayed, so as per that i realize i need do some settings or pass some commands to tell the debugger to break during the exception and catch it.

    how do set the debugger to break during the std:bad_alloc ??signal???



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


  • Qt Champions 2018

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

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


  • Qt Champions 2018

    @mpergand Awesome! I didn't know you can catch these. Thanks for your explanation.



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

    0_1562030120509_heob_results.png

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


  • Qt Champions 2018

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



  • @fcarney @jsulm

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


  • Qt Champions 2018

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