Qt program crashes after some minutes run time!



  • Hi, I have a program but after some time crashes and i cannot find what is wrong at my code, the debugger jumps to qstring.h and shows the below line:

     inline QString::~QString() { if (!d->ref.deref()) free(d); }
    

    Can someone tell me what can i do to find out the problem ?


  • Moderators

    First thing to do is look at the stack trace and see if some of your code is there.


  • Lifetime Qt Champion

    Hi,

    To add to @Chris-Kawa , and since it happens after a moment, check for a memory leak in your code.



  • @SGaist @Chris-Kawa

    i found that in this line i have the corruption

     Lon.setNum(a.lon);
    

    but i checked the number and this is a number, i cannot understand why crashes, if the number is correct.


  • Moderators

    It's really hard to help you with a one-liner. There are number of problems that can cause this. Lon, whatever that is, may be invalid, a, whatever that is, may be invalid. Either of setNum or lon can have corrupting side-effects. There can be unrelated memory corruption that happens to manifest here. Without more details it's a pure guesswork on our side.



  • @Chris-Kawa

    You have right, the code it's enormous to be posted in a thread.
    It is also difficult to me , to find what is going wrong! But i try to post something more helpful!


  • Lifetime Qt Champion

    If it's that big, try running a tool like cppcheck to see if you missed something



  • @SGaist runs this also at Linux?



  • It definitely sounds like you have a pointer that is getting trashed or a serious memory leak.

    One way to combat this would be tools to check memory use. However another and perhaps simpler approach is to litter your code with qDebug() calls with unique strings. Some times I just start doing things like:

    qDebug() << "A";

    ... some code ...

    qDebug << "B";

    etc...

    Try to figure out what the last thing printed is. Then work your way backward and set debug break points. At least using qDebug you can narrow the search area.



  • @SysTech i made also this, but seems to be a problem at
    a.lon and a.lat.

    a is a structure
    i call this

        if(AirplanesVector.size()>0&&flightNameChoosed.size()>2) {
    int i = ReturnIndexOfSearch(flightNameChoosed);
    if(i>-1){
    aircraft a = AirplanesVector[i];
        qDebug()<<"LON: " << a.lon <<"LAT: " <<a.lat;
    

    And after a long time crashes.



  • I made some changes and seems to be very stable now,

     aircraft *a = new aircraft[1];
            a= &AirplanesVector[i];
     qDebug()<<"LON: " << a->lon <<"LAT: " <<a->lat;
     a=0;

  • Moderators

    You just created a memory leak.
    If anything it should be this:

    aircraft *a = &AirplanesVector[i];
    

    Previously you were making a copy, so it seems your aircraft type is buggy (no proper copy constructor/assignment operator?).



  • @Chris-Kawa but now it is working stable, the program don't crash, this is the aircraft structure from ramirez:

    struct aircraft {
    uint32_t addr;      /* ICAO address */
    char hexaddr[7];    /* Printable ICAO address */
    char flight[9];     /* Flight number */
    int altitude;       /* Altitude */
    int speed;          /* Velocity computed from EW and NS components. */
    int track;          /* Angle of flight. */
    time_t seen;        /* Time at which the last packet was received. */
    long messages;      /* Number of Mode S messages received. */
    /* Encoded latitude and longitude as extracted by odd and even
     * CPR encoded messages. */
    int odd_cprlat;
    int odd_cprlon;
    int even_cprlat;
    int even_cprlon;
    double lat, lon;    /* Coordinated obtained from CPR encoded data. */
    long long odd_cprtime, even_cprtime;
    struct aircraft *next; /* Next aircraft in our linked list. */
    }; 
    

    this is my project https://github.com/ntosis/GUI_dump1090


  • Moderators

    Working stable but leaking memory. You have a new without a delete. It's not Java. It's a memory leak

        aircraft *a = new aircraft[1];   //creates new instance, what for if you don't even use it?
        a= &AirplanesVector[i]; //assigns to a some other instance, previous instance is now lost and leaked
        ...
        a=0; //this does not free any memory in c++
    

    You have a next pointer in your linked list structure. If you just copy it you are also copying that pointer. then you have two nodes in your list pointing to the same next item and this can lead to a double delete (crash) if not carefully handled.

    Btw. keeping a linked list elements in a vector seems like an odd hing to do. Why not just make it a vector then?



  • if i use delete, then this line make the program to crash again, i understand what do you mean. i try to find a solution! this a code from an other developer so he has an other concept!


  • Moderators

    Don't use delete cause you're deleting the wrong thing. Just don't create an extra instance. Do it like I posted before:

    aircraft *a = &AirplanesVector[i]; 
    qDebug()<<"LON: " << a->lon <<"LAT: " <<a->lat;
    

    That's it. No memory leak and no needless instances.



  • @Chris-Kawa thank you, now i run the program to see if its ok!



  • @Chris-Kawa

    Hi again, the program runs stable until is going in backstage, if for example i open the Mozilla to surfing, then the program crashes!


  • Lifetime Qt Champion

    Can your share the backtrace of your crash ?



  • @SGaist i try to stimulate the crash again and i try to post it! thank you


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.