Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QApplication::~QApplication destructor crashes(Segmentation Fault) when exit program
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 4.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sambath
    wrote on 18 Sept 2013, 07:26 last edited by
    #1

    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
    {
    Q_OBJECT

    public:
    HuGraphics();
    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);

    private:

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

    };

    HuGraphics::HuGraphics()
    {
    Humain_screen = new QWidget;
    GuiStack = new QStackedLayout;
    GuiStack->setStackingMode(QStackedLayout::StackAll);
    GvLayout = new QVBoxLayout;
    }

    void HuGraphics::HuDrawGraphics(int width, int height)
    {
    GuiStack->addWidget(Humain_screen);
    GvLayout->addLayout(GuiStack);
    GvLayout->setContentsMargins(0, 0, 0, 0);
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->resize(width, height);
    this->setLayout(GvLayout);
    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]->setText(uexTxtCompParams);
    hu_graphicobj->textlabel[labelId]->setStyleSheet("color: rgb(255,255,255)");
    hu_graphicobj->textlabel[labelId]->setFont(QFont("Arial Black",16));
    hu_graphicobj->textlabel[labelId]->show();
    }

    void HuGraphics::HuHideText(int labelId,HuGraphics* hu_graphicobj)
    {
    hu_graphicobj->textlabel[labelId]->hide();
    }

    extern "C" int hugfx_dispgraphics(int width,int height)
    {
    hu_graphicobj.HuDrawGraphics(width,height);//for initialising graphic layer
    hu_graphicobj.show();
    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]

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on 18 Sept 2013, 08:06 last edited by
      #2

      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sambath
        wrote on 18 Sept 2013, 11:38 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Moschops
          wrote on 18 Sept 2013, 11:44 last edited by
          #4

          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.

          1 Reply Last reply
          0

          1/4

          18 Sept 2013, 07:26

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved