QApplication::~QApplication destructor crashes(Segmentation Fault) when exit program

  • Hi,

    We have developed QT 4.8.1 based simple program which draws couple of Text widgets and hides them. Everything works well until i quit or exit the program. This is a multi-threaded program.
    During exit sequence QApplication::~QApplication destructor is throwing segmentation fault. I am from C background and tried to debug this program with different approach but couldnt succeed.
    Below is the sample program which causes this error. Pls help in fixing the root cause of the error.
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    #include <linux/input.h>
    #include <stdbool.h>
    #include <stdint.h>
    #include <unistd.h>

    #include <QWidget>
    #include <QLabel>
    #include <QStackedLayout>
    #include <QApplication>

    int argc; char *argv[1];
    QApplication app(argc, argv);
    HuGraphics hu_graphicobj;
    signal hu_emitsignl;

    class HuGraphics : public QWidget

    void HuDrawGraphics(int width,int height);
    int hugfx_dispgraphics(int,int);
    QLabel *textlabel[4];

    private slots:
    void HuDrawText(int labelId,char* uexTxtCompParams,int xPos,int yPos,HuGraphics* hu_graphicobj);
    void HuHideText(int labelId,HuGraphics* hu_graphicobj);


    QWidget *Humain_screen;
    QLabel *HuBackgroundImgLabel;
    QStackedLayout *GuiStack;
    QVBoxLayout *GvLayout;


    Humain_screen = new QWidget;
    GuiStack = new QStackedLayout;
    GvLayout = new QVBoxLayout;

    void HuGraphics::HuDrawGraphics(int width, int height)
    GvLayout->setContentsMargins(0, 0, 0, 0);
    this->resize(width, height);
    this->setStyleSheet("background-color: rgb(173,153,96)");

    void HuGraphics::HuDrawText(int labelId,char* uexTxtCompParams,int xPos,int yPos,HuGraphics* hu_graphicobj)
    hu_graphicobj->textlabel[labelId] = new QLabel(this->Humain_screen);
    hu_graphicobj->textlabel[labelId]->move(xPos, yPos);
    hu_graphicobj->textlabel[labelId]->setStyleSheet("color: rgb(255,255,255)");
    hu_graphicobj->textlabel[labelId]->setFont(QFont("Arial Black",16));

    void HuGraphics::HuHideText(int labelId,HuGraphics* hu_graphicobj)

    extern "C" int hugfx_dispgraphics(int width,int height)
    hu_graphicobj.HuDrawGraphics(width,height);//for initialising graphic layer;
    return app.exec();

    int main ( int argc, char *argv[] )
    hugfx_drawtxt ( 0, "address", 200, 200 );
    hugfx_drawtxt ( 1, "netmask", 200, 230 );
    hugfx_drawtxt ( 2, "broadcast", 200, 260 );
    hugfx_drawtxt ( 3, "gateway", 200, 290 );
    hugfx_dispgraphics ( 1024, 768 );
    /*app.exec() is terminated using qApp->quit() call from different thread. I tried with app.quit() also */

    [edit, code tags added, koahnig]

  • Moderators

    welcome to devnet

    Please check out the forum guidelines on use of code tags. I have introduced them you this time.

    From your description above, it sounds like you are freeing multiple times the same memory. That is not healthy in pure C either.

    It might be a bit irritating for you that Qt is freeing memory for you. You can hand over the handling to a parent, which will make sure to clean-up all children's allocated memory when it is destroyed itself. When you are handing over the same pointer more than once, this is creating a collision when the objects are destroyed. The first destruction is ok, but generates a problem for subsequent destruction of the same memory.

    A quick look shows that you have separated memory allocation and subsequent handling (line 47 and line 55). The constructor is called only once, but you may call method "HuDrawGraphics" more than once. That may be the source of your problem.

  • koahnig,
    thank you very much. I agree with you. Even i realized that QApplication destructor is called more than once and causes this issue. But i dont know how to fix it(Am very fresh to CPP and QT. My team also not familiar in this).
    Could you pls throw few samples on how to do it.
    Moreover, we are not deleting/freeing any of the objects we have created in HuGraphics constructor.

  • At risk of teaching you to suck eggs, have you run a version with debug symbols enabled through valgrind and/or watched it crash with gbd? I'm sure I've used valgrind before to identify multiple frees of the same memory.

Log in to reply

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