Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions
QApplication::~QApplication destructor crashes(Segmentation Fault) when exit program
Sambath last edited by
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.
int argc; char *argv;
QApplication app(argc, argv);
class HuGraphics : public QWidget
void HuDrawGraphics(int width,int height);
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);
void HuGraphics::HuDrawText(int labelId,char* uexTxtCompParams,int xPos,int yPos,HuGraphics* hu_graphicobj)
hu_graphicobj->textlabel[labelId] = new QLabel(this->Humain_screen);
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
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]
koahnig last edited by
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.
Sambath last edited by
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.
Moschops last edited by
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.