Results 1 to 3 of 3

Thread: [solved] Any idea to prevent that crash?

  1. #1
    Join Date
    Apr 2006
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [solved] Any idea to prevent that crash?

    I have here a cut-down example of a crash indirect caused by setAttribute(Qt::WA_DeleteOnClose) and I do not know a nice way to prevent it.

    To reproduce:
    Compile that snipplet and start it. Then click into the window which shows up (it contains a pushbutton) and after that close the window (where you clicked into) by clicking on the bix X in the title line.

    The crash happens because that attribute "Qt::WA_DeleteOnClose" calls the destructor of the main window. Since the small dialog has a parent, it is deleted too. Now it has its own exec() call where it loops.

    Now I do not want to drop that "Qt::WA_DeleteOnClose" line, because this would mean unused memory which is still allocated and the application grows and grows ...

    I also would like to show the child dialog centered above the main window and since I am lazy, is simple use the parent widget as a parameter during start.

    I also would like to avoid a modal dialog.

    If I could drop one of these three requirements, the crash would go.

    One way could be to clear the parent of the child window. This would mean a lot of coding, since I need to know which child windows exists (there could be many, not just one), but as i wrote: I am lazy ...

    Another way could be to drop the exec() call in the opened dialog, which would mean rearranging the logic in the caller, again this needs to handle a collection of a opened subdialogs ... I don't like that, since I am lazy.

    Does anyone have another idea?

    Qt Code:
    1. #include <Qt/qapplication.h>
    2. #include <Qt/qpushbutton.h>
    3. #include <Qt/qmainwindow.h>
    4. #include <Qt/qdialog.h>
    5. #include <Qt/qlabel.h>
    6. #include <Qt/qlayout.h>
    7. #include <Qt/qevent.h>
    8.  
    9. class SomeMainWin : public QMainWindow {
    10. Q_OBJECT;
    11. public:
    12. SomeMainWin();
    13. virtual ~SomeMainWin();
    14. virtual void closeEvent(QCloseEvent *event);
    15. QPushButton *_openDialog;
    16.  
    17. public slots:
    18. void openAnDialog();
    19. };
    20.  
    21. class SomeDialog : public QDialog {
    22. public:
    23. SomeDialog(QWidget *parent);
    24. virtual ~SomeDialog();
    25. int run();
    26. };
    27.  
    28. int main(int argc, char **argv)
    29. {
    30. QApplication app(argc, argv);
    31. SomeMainWin *myMainWin = new SomeMainWin();
    32. myMainWin->show();
    33. app.exec();
    34. exit(0);
    35. }
    36.  
    37. SomeMainWin::SomeMainWin()
    38. {
    39. setAttribute(Qt::WA_DeleteOnClose);
    40. _openDialog = new QPushButton("Open");
    41. connect(_openDialog, SIGNAL(clicked()), this, SLOT(openAnDialog()));
    42. setCentralWidget(_openDialog);
    43. }
    44.  
    45. SomeMainWin::~SomeMainWin()
    46. {
    47. }
    48.  
    49. void SomeMainWin::closeEvent(QCloseEvent *event)
    50. {
    51. event->accept();
    52. }
    53.  
    54. void SomeMainWin::openAnDialog()
    55. {
    56. SomeDialog SomeDialog(this);
    57. if(SomeDialog.run() == QDialog::Accepted) {
    58. // ... do this an that
    59. }
    60. }
    61.  
    62. SomeDialog::SomeDialog(QWidget *parent)
    63. : QDialog(parent)
    64. {
    65. QVBoxLayout *layout = new QVBoxLayout();
    66. setLayout(layout);
    67.  
    68. QLabel *label = new QLabel("Hello");
    69. layout->addWidget(label);
    70. }
    71.  
    72. SomeDialog::~SomeDialog()
    73. {
    74. }
    75.  
    76. int SomeDialog::run()
    77. {
    78. show();
    79. exec();
    80. }
    81.  
    82. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    the crash looks like this (it is Suse Linux)
    $ ./a.out
    QObject: Do not delete object, 'unnamed', during its event handler!
    *** glibc detected *** ./a.out: double free or corruption (out): 0x00007fffc1a87630 ***
    Last edited by Wurgl; 13th January 2011 at 00:23. Reason: Problem solved ...

  2. #2
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Any idea to prevent that crash?

    Hi,

    in the closeEvent() of the dialog, you could do this.deleteLater()

    Regards,
    Marc

  3. #3
    Join Date
    Apr 2006
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Any idea to prevent that crash?

    You mean this way?

    Qt Code:
    1. SomeMainWin::SomeMainWin()
    2. {
    3. // setAttribute(Qt::WA_DeleteOnClose);
    4. _openDialog = new QPushButton("Open");
    5. connect(_openDialog, SIGNAL(clicked()), this, SLOT(openAnDialog()));
    6. setCentralWidget(_openDialog);
    7. }
    8.  
    9. void SomeMainWin::closeEvent(QCloseEvent *event)
    10. {
    11. event->accept();
    12. deleteLater();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Sadly it does not help, still the same crash.


    Added after 34 minutes:


    But this way it works.

    Qt Code:
    1. void SomeMainWin::closeEvent(QCloseEvent *event)
    2. {
    3. SomeDialog *dialog;
    4. while((dialog = findChild<SomeDialog *>()) != 0) {
    5. dialog->setParent(0);
    6. dialog->close();
    7. }
    8. event->accept();
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Wurgl; 13th January 2011 at 00:23.

Similar Threads

  1. A Hippie Idea
    By qtoptus in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2010, 23:28
  2. ???any one have idea about this ???
    By damodharan in forum Qt Programming
    Replies: 1
    Last Post: 29th May 2010, 09:08
  3. skins idea
    By CopyrightPhilly in forum General Discussion
    Replies: 1
    Last Post: 11th November 2007, 15:16
  4. No idea about QGraphicsView
    By Shawn in forum Qt Programming
    Replies: 2
    Last Post: 27th June 2007, 10:02
  5. UI Idea please
    By munna in forum General Discussion
    Replies: 1
    Last Post: 9th May 2006, 11:14

Tags for this Thread

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.