Results 1 to 2 of 2

Thread: Segfaults when DeleteOnClose window attribute is set

  1. #1
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    76
    Thanked 37 Times in 32 Posts

    Default Segfaults when DeleteOnClose window attribute is set

    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
    Qt Code:
    1. #include <QApplication>
    2. #include <QDialog>
    3. #include "MainDialog.h"
    4. #include "NewDialog.h"
    5.  
    6. int main( int argc, char *argv[] ){
    7. QApplication app( argc, argv );
    8.  
    9. NewDialog newDialog;
    10. newDialog.show();
    11.  
    12. MainDialog mainDialog;
    13. mainDialog.show();
    14.  
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 
    When I set the WindowAttribute of the MainDialog to WA_DeletOnClose.
    I get a segfault when both the windows are closed.

    Here is the code of the MainDialog.cpp
    Qt Code:
    1. #include <QtDebug>
    2. #include "NewDialog.h"
    3.  
    4. #include "MainDialog.h"
    5.  
    6. MainDialog::MainDialog( QWidget *parent )
    7. :QDialog( parent ) {
    8. setupUi( this );
    9. setAttribute( Qt::WA_DeleteOnClose ); // Adding this give segfaults
    10.  
    11. newDlg =0;
    12. qDebug()<< __FILE__ << " [" << __LINE__ << "]" << __FUNCTION__ << "() : ";
    13. connect( showDialogButton, SIGNAL(clicked()), this, SLOT(showDialog()) );
    14. }
    15.  
    16. MainDialog::~MainDialog(){
    17. }
    18.  
    19. void MainDialog::showDialog(){
    20. qDebug()<< __FILE__ << " [" << __LINE__ << "]" << __FUNCTION__ << "() : ";
    21. if( ! newDlg ){
    22. newDlg = new NewDialog( this );
    23. }
    24. newDlg->show();
    25. }
    To copy to clipboard, switch view to plain text mode 
    PFA source of the program.
    Am I missing something ?
    Attached Files Attached Files
    We can't solve problems by using the same kind of thinking we used when we created them

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Segfaults when DeleteOnClose window attribute is set

    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.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    sunil.thaha (9th November 2006)

Similar Threads

  1. cannot make a main window modal
    By Dark_Tower in forum Qt Programming
    Replies: 12
    Last Post: 23rd March 2006, 11:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.