PDA

View Full Version : Segfaults when DeleteOnClose window attribute is set



sunil.thaha
9th November 2006, 07:34
Hi guys,

I am using Qt 4.2 on Cent OS ( 64 Bit )

I wrote a simple program for demonstration purpose.
Here is How the program works.

There are two Classes - MainDialog, NewDialog

In the main I create two dialogs, that is it ;).
Here is my main


#include <QApplication>
#include <QDialog>
#include "MainDialog.h"
#include "NewDialog.h"

int main( int argc, char *argv[] ){
QApplication app( argc, argv );

NewDialog newDialog;
newDialog.show();

MainDialog mainDialog;
mainDialog.show();

return app.exec();
}

When I set the WindowAttribute of the MainDialog to WA_DeletOnClose.
I get a segfault when both the windows are closed. :confused:

Here is the code of the MainDialog.cpp


#include <QtDebug>
#include "NewDialog.h"

#include "MainDialog.h"

MainDialog::MainDialog( QWidget *parent )
:QDialog( parent ) {
setupUi( this );
setAttribute( Qt::WA_DeleteOnClose ); // Adding this give segfaults

newDlg =0;
qDebug()<< __FILE__ << " [" << __LINE__ << "]" << __FUNCTION__ << "() : ";
connect( showDialogButton, SIGNAL(clicked()), this, SLOT(showDialog()) );
}

MainDialog::~MainDialog(){
}

void MainDialog::showDialog(){
qDebug()<< __FILE__ << " [" << __LINE__ << "]" << __FUNCTION__ << "() : ";
if( ! newDlg ){
newDlg = new NewDialog( this );
}
newDlg->show();
}

PFA source of the program.
Am I missing something ?

jpn
9th November 2006, 08:04
You are allocating the widgets on the stack in main(), so the objects get destructed two times; 1) upon close 2) after going out of scope.

In other words, you must not set the Qt::WA_DeleteOnClose attribute for a widget allocated on stack.