Results 1 to 10 of 10

Thread: Unexplained Segmentation Fault with QDialog::show() and QDialog::exec()

  1. #1
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Unexplained Segmentation Fault with QDialog::show() and QDialog::exec()

    In a Qt 4.7.1 project in Ubuntu 10.10 x64, I have subclassed QDialog and am calling it in the following way:

    Qt Code:
    1. //Create a dialog window to get the requested difficulty
    2. this->generateNewDialog = new GenerateNewDialog(this);
    3. connect(this->generateNewDialog, SIGNAL(difficultySelected(int)), this, SLOT(generationDifficultySelected(int)));
    4.  
    5. this->generateNewDialog->exec();
    To copy to clipboard, switch view to plain text mode 

    Without fail, this always produces a segmentation fault and crashes my program on the exec() call (not before). Looking at the call stack, the fault occurs at:

    QWidgetPrivate::create_sys
    QWidget::create
    QWidget::setVisible
    QDialog::setVisible
    show
    QDialog::exec

    Does anyone see a reason for the behavior? I don't.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unexplained Segmentation Fault with QDialog::show() and QDialog::exec()

    Can you post the code for GenerateNewDialog?
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Unexplained Segmentation Fault with QDialog::show() and QDialog::exec()

    header file
    Qt Code:
    1. #ifndef GENERATENEWDIALOG_H
    2. #define GENERATENEWDIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QSpinBox>
    6. #include <QDialogButtonBox>
    7. #include <QVBoxLayout>
    8. #include <QHBoxLayout>
    9. #include <QLabel>
    10. #include <QSpinBox>
    11. #include <QPushButton>
    12. #include <QDebug>
    13. class GenerateNewDialog : public QDialog
    14. {
    15. Q_OBJECT
    16. private:
    17. void setupInterface();
    18. void setupActions();
    19. QVBoxLayout * m_mainLayout;
    20. QHBoxLayout * m_firstSetting;
    21. QLabel * m_label1;
    22. QSpinBox * m_difficultyPicker;
    23. QPushButton * m_cancel, * m_start;
    24. QDialogButtonBox *buttonBox;
    25. int m_difficulty;
    26. public:
    27. explicit GenerateNewDialog(QWidget *parent = 0);
    28. virtual ~GenerateNewDialog();
    29. signals:
    30. void difficultySelected(int difficulty);
    31. private slots:
    32. void accept();
    33. void reject();
    34. public slots:
    35.  
    36. };
    37.  
    38. #endif // GENERATENEWDIALOG_H
    To copy to clipboard, switch view to plain text mode 

    class file
    Qt Code:
    1. #include "generatenewdialog.h"
    2.  
    3. GenerateNewDialog::GenerateNewDialog(QWidget *parent) :
    4. QDialog(parent, Qt::Sheet)
    5. {
    6. setupInterface();
    7. setupActions();
    8. }
    9. //----------------------------------------------------------------------------
    10. // Constructor & destructor.
    11.  
    12. GenerateNewDialog::~GenerateNewDialog(void) {
    13. // Is automatically deleted, because it's a child of of the main widget.
    14. //delete m_mainLayout;
    15. delete m_firstSetting;
    16. delete m_label1;
    17. delete m_difficultyPicker;
    18. delete m_cancel;
    19. delete m_start;
    20. }
    21.  
    22.  
    23. //----------------------------------------------------------------------------
    24. // Private slots.
    25.  
    26. /**
    27.  * The dialog was accepted. Emit the newGame() signal.
    28.  */
    29. void GenerateNewDialog::accept(void) {
    30. m_difficulty = m_difficultyPicker->value();
    31.  
    32. close();
    33.  
    34. emit difficultySelected(m_difficulty);
    35. }
    36.  
    37.  
    38. //----------------------------------------------------------------------------
    39. // Private methods.
    40.  
    41. /**
    42.  * Create the UI.
    43.  */
    44. void GenerateNewDialog::setupInterface(void) {
    45. setContextMenuPolicy(Qt::NoContextMenu);
    46. setModal(true);
    47.  
    48. // Configure layouts.
    49. m_mainLayout = new QVBoxLayout(this);
    50. m_firstSetting = new QHBoxLayout();
    51. m_mainLayout->addLayout(m_firstSetting);
    52.  
    53. // Set up the buttons.
    54. this->buttonBox = new QDialogButtonBox(this);
    55. this->buttonBox->setOrientation(Qt::Horizontal);
    56. m_cancel = new QPushButton(tr("&Cancel"));
    57. this->buttonBox->addButton(m_cancel, QDialogButtonBox::RejectRole);
    58. m_start = new QPushButton(tr("&Start!"));
    59. m_start->setDefault(true);
    60. this->buttonBox->addButton(m_start, QDialogButtonBox::AcceptRole);
    61. m_mainLayout->addWidget(buttonBox);
    62.  
    63. // Difficulty label.
    64. m_label1 = new QLabel(this);
    65. m_label1->setText(tr("Difficulty level"));
    66. m_firstSetting->addWidget(m_label1);
    67. m_firstSetting->setAlignment(m_label1, Qt::AlignLeft);
    68.  
    69. // Difficulty spinbox.
    70. m_difficultyPicker = new QSpinBox(this);
    71. m_difficultyPicker->setMinimum(1);
    72. m_difficultyPicker->setMaximum(5);
    73. m_difficultyPicker->setSingleStep(1);
    74. m_difficultyPicker->setValue(1);
    75. m_firstSetting->addWidget(m_difficultyPicker);
    76. m_firstSetting->setAlignment(m_difficultyPicker, Qt::AlignRight);
    77.  
    78. // Connect the signals from the buttonbox to the GenerateNewDialog.
    79. connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    80. connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    81. }
    82. void GenerateNewDialog::setupActions(){
    83. // Connect the signals from the buttonbox to the NewGameDialog.
    84. connect(this->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    85. connect(this->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    86. }
    87.  
    88. void GenerateNewDialog::reject(){
    89. qDebug() << "GenerateNewDialog received reject signal.";
    90. close();
    91. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unexplained Segmentation Fault with QDialog::show() and QDialog::exec()

    There are many strange things!

    1) In the destructor you delete the children created in setupInterface();

    2) you connect twice accepted and rejected signals to slots;

    3) You emit signal on accept(), should be simpler to read property in calling code after exec returns.
    Last edited by mcosta; 26th February 2011 at 00:40. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Unexplained Segmentation Fault with QDialog::show() and QDialog::exec()

    Thank you for your ideas.

    1) & 4) The reason I emit a signal on accept() is because I need to get the index of the spinbox that was selected. If there's a better way to do this, let me know!

    2) Why would this be a bad thing? I need to free the memory before the dialog ceases to exist, right?

    3) Yes, that was just a mistake.

    Revised code that still produces a seg fault:
    Qt Code:
    1. #ifndef GENERATENEWDIALOG_H
    2. #define GENERATENEWDIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QSpinBox>
    6. #include <QDialogButtonBox>
    7. #include <QVBoxLayout>
    8. #include <QHBoxLayout>
    9. #include <QLabel>
    10. #include <QSpinBox>
    11. #include <QPushButton>
    12. #include <QDebug>
    13. class GenerateNewDialog : public QDialog
    14. {
    15. Q_OBJECT
    16. private:
    17. void setupInterface();
    18. void setupActions();
    19. QVBoxLayout * m_mainLayout;
    20. QHBoxLayout * m_firstSetting;
    21. QLabel * m_label1;
    22. QSpinBox * m_difficultyPicker;
    23. QPushButton * m_cancel, * m_start;
    24. QDialogButtonBox *buttonBox;
    25. int m_difficulty;
    26. public:
    27. explicit GenerateNewDialog(QWidget *parent = 0);
    28. virtual ~GenerateNewDialog();
    29. signals:
    30. void difficultySelected(int difficulty);
    31. private slots:
    32. virtual void accept();
    33. virtual void reject();
    34. public slots:
    35.  
    36. };
    37.  
    38. #endif // GENERATENEWDIALOG_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "generatenewdialog.h"
    2.  
    3. GenerateNewDialog::GenerateNewDialog(QWidget *parent) :
    4. QDialog(parent, Qt::Sheet)
    5. {
    6. setupInterface();
    7. setupActions();
    8. }
    9. //----------------------------------------------------------------------------
    10. // Constructor & destructor.
    11.  
    12. GenerateNewDialog::~GenerateNewDialog(void) {
    13. // Is automatically deleted, because it's a child of of the main widget.
    14. //delete m_mainLayout;
    15. delete m_firstSetting;
    16. delete m_label1;
    17. delete m_difficultyPicker;
    18. delete m_cancel;
    19. delete m_start;
    20. }
    21.  
    22.  
    23. //----------------------------------------------------------------------------
    24. // Private slots.
    25.  
    26. /**
    27.  * The dialog was accepted. Emit the newGame() signal.
    28.  */
    29. void GenerateNewDialog::accept(void) {
    30. m_difficulty = m_difficultyPicker->value();
    31.  
    32. emit difficultySelected(m_difficulty);
    33. QDialog::accept();
    34. }
    35.  
    36.  
    37. //----------------------------------------------------------------------------
    38. // Private methods.
    39.  
    40. /**
    41.  * Create the UI.
    42.  */
    43. void GenerateNewDialog::setupInterface(void) {
    44. setContextMenuPolicy(Qt::NoContextMenu);
    45. setModal(true);
    46.  
    47. // Configure layouts.
    48. m_mainLayout = new QVBoxLayout(this);
    49. m_firstSetting = new QHBoxLayout();
    50. m_mainLayout->addLayout(m_firstSetting);
    51.  
    52. // Set up the buttons.
    53. this->buttonBox = new QDialogButtonBox(this);
    54. this->buttonBox->setOrientation(Qt::Horizontal);
    55. m_cancel = new QPushButton(tr("&Cancel"));
    56. this->buttonBox->addButton(m_cancel, QDialogButtonBox::RejectRole);
    57. m_start = new QPushButton(tr("&Start!"));
    58. m_start->setDefault(true);
    59. this->buttonBox->addButton(m_start, QDialogButtonBox::AcceptRole);
    60. m_mainLayout->addWidget(buttonBox);
    61.  
    62. // Difficulty label.
    63. m_label1 = new QLabel(this);
    64. m_label1->setText(tr("Difficulty level"));
    65. m_firstSetting->addWidget(m_label1);
    66. m_firstSetting->setAlignment(m_label1, Qt::AlignLeft);
    67.  
    68. // Difficulty spinbox.
    69. m_difficultyPicker = new QSpinBox(this);
    70. m_difficultyPicker->setMinimum(1);
    71. m_difficultyPicker->setMaximum(5);
    72. m_difficultyPicker->setSingleStep(1);
    73. m_difficultyPicker->setValue(1);
    74. m_firstSetting->addWidget(m_difficultyPicker);
    75. m_firstSetting->setAlignment(m_difficultyPicker, Qt::AlignRight);
    76. }
    77. void GenerateNewDialog::setupActions(){
    78. // Connect the signals from the buttonbox to the NewGameDialog.
    79. connect(this->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    80. connect(this->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    81. }
    82.  
    83. void GenerateNewDialog::reject(){
    84. qDebug() << "GenerateNewDialog received reject signal.";
    85. QDialog::reject();
    86. close();
    87. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unexplained Segmentation Fault with QDialog::show() and QDialog::exec()

    Quote Originally Posted by powerofpi View Post
    Thank you for your ideas.

    1) & 4) The reason I emit a signal on accept() is because I need to get the index of the spinbox that was selected. If there's a better way to do this, let me know!

    2) Why would this be a bad thing? I need to free the memory before the dialog ceases to exist, right?
    2) When a QObject are destroyed, it delete all children. You have not need to delete them explicitly

    For the 1) I suggest you to define a difficulty() public methods that returns m_difficulty and call it after exec from calling code

    Qt Code:
    1. // For modal Dialog (exec()) you can use a stack variable
    2. GenerateNewDialog dlg(this);
    3.  
    4. if (QDialog::Accepted == generateNewDialog.exec()) {
    5. difficulty = dlg.difficulty();
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by mcosta; 26th February 2011 at 00:56. Reason: spelling corrections
    A camel can go 14 days without drink,
    I can't!!!

  7. #7
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Unexplained Segmentation Fault with QDialog::show() and QDialog::exec()

    2) When a QObject are destroyed, it delete all children. You have not need to delete them explicitly
    Thanks for that, I wasn't aware of it!

    For the 1) I suggest you to define a difficulty() public methods that returns m_difficulty and call it after exec from calling code
    I would definitely do that, if exec() didn't cause a segmentation fault

    As some additional information, I tried to recreate the error with a very small program consisting of only a button with the same dialog window. In this case there was no seg fault and the window was displayed successfully. Could the complication of my program's main window (4 QDockWidgets and a QStatusBar) be causing the seg fault in my main program? Otherwise I used the same code and called it in the same way, so I'm getting more confused...

  8. #8
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unexplained Segmentation Fault with QDialog::show() and QDialog::exec()

    Probably there is something wrong in your code. Check where you explicitly delete Widget and verify if there are logical errors.
    A camel can go 14 days without drink,
    I can't!!!

  9. #9
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Unexplained Segmentation Fault with QDialog::show() and QDialog::exec()

    Here is the relevant part of my main window code... I'm registering a generateNew slot to receive signals triggered by a new action. Then elsewhere I'm pressing the new button (in a dock widget), causing me to enter my generateNew() slot, and then the seg fault is always occurring on the call to exec(). Could there be a problem with a button press in a dock widget triggering an action and causing a dialog window in the main widget?

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QDesktopWidget>
    3. #include <QApplication>
    4. #include <QResizeEvent>
    5. #include "engine/engine.h"
    6. #include "storage/storage.h"
    7. #include "gui/actionmanager.h"
    8.  
    9. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    10. {
    11. //Test something manually
    12. //test();
    13.  
    14. //Create actions
    15. createActions();
    16.  
    17. //Create window internals
    18. createMainSceneAndView();
    19.  
    20. //Create dock windows
    21. createDockWindows();
    22.  
    23. //Additional Settings for this Window
    24. configureWindow();
    25.  
    26. restoreSettings();
    27.  
    28. show();
    29. }
    30. void MainWindow::createActions(){
    31.  
    32. ActionManager::generateNewAction = new QAction(this);
    33. connect(ActionManager::generateNewAction, SIGNAL(triggered()), this, SLOT(generateNew()));
    34.  
    35. }
    36. void MainWindow::generateNew(){
    37. qDebug() << "MainWindow got generate new event.";
    38.  
    39. //Create a dialog window to get the requested difficulty
    40. this->generateNewDialog = new GenerateNewDialog(this);
    41. connect(this->generateNewDialog, SIGNAL(difficultySelected(int)), this, SLOT(generationDifficultySelected(int)));
    42.  
    43. this->generateNewDialog->exec();
    44. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Feb 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Unexplained Segmentation Fault with QDialog::show() and QDialog::exec()

    I am having similar problems with a class that inherits from QDialog. In my program i have a QVector<dialog*> with pointers to a number of different instances of the QDialog-derived class. I have a QComboBox that I want to use to activate (show()) these dialogs.

    I have two items in the QComboBox and when I activate one dialog everything is fine. But opening the second one leads to a segmentation fault. Note that it does not matter which of the dialogs I open first. But the segmentation fault itself is not caused by me calling show()...

    #0 0x00000008 in ?? () at c:/Qt/2010.05/qt/include/QtGui/../../src/gui/kernel/qwidget.h:487
    #1 0x00bc0119 in QPainter (this=0x22d468, pd=0x9f504a0) at painting\qpainter.cpp:1489
    #2 0x00aed236 in QWidgetPrivate::drawWidget (this=0x9f51600, pdev=0x9fa27c8, rgn=..., offset=..., flags=5, sharedPainter=0x0, backingStore=0x9fa2780) at kernel\qwidget.cpp:5397
    #3 0x00c74b31 in QWidgetBackingStore::sync (this=0x9fa2780) at painting\qbackingstore.cpp:1328
    #4 0x00ae4f50 in QWidgetPrivate::syncBackingStore (this=0x9f51600) at kernel\qwidget.cpp:1805
    #5 0x00af4e75 in QWidget::event (this=0x9f50498, event=0x9f4d360) at kernel\qwidget.cpp:8480
    #6 0x00aa9706 in QApplicationPrivate::notify_helper (this=0x3e4c18, receiver=0x9f50498, e=0x9f4d360) at kernel\qapplication.cpp:4396
    #7 0x00aa9586 in QApplication::notify (this=0x22fe98, receiver=0x9f50498, e=0x9f4d360) at kernel\qapplication.cpp:4361
    #8 0x6a1ff9dc in QCoreApplication::notifyInternal (this=0x22fe98, receiver=0x9f50498, event=0x9f4d360) at kernel\qcoreapplication.cpp:732
    #9 0x6a265fbc in QCoreApplication::sendEvent (receiver=0x9f50498, event=0x9f4d360) at kernel//qcoreapplication.h:215
    #10 0x6a200a87 in QCoreApplicationPrivate::sendPostedEvents (receiver=0x0, event_type=0, data=0x3e5798) at kernel\qcoreapplication.cpp:1373
    #11 0x6a222d2e in qt_internal_proc (hwnd=0x105026c, message=1025, wp=0, lp=0) at kernel\qeventdispatcher_win.cpp:503
    #12 0x7e418734 in USER32!GetDC () from C:\WINDOWS\system32\user32.dll
    #13 0x0105026c in QGraphicsScenePrivate::drawSubtreeRecursive (this=0x6a222a8a, item=0x105026c, painter=0x401, viewTransform=0x0, exposedRegion=0x0, widget=0x22fcdc, parentOpacity=1.6853403962263272e-307, effectTransform=0x14) at graphicsview\qgraphicsscene.cpp:4717
    #14 0x7e418816 in USER32!GetDC () from C:\WINDOWS\system32\user32.dll
    #15 0x6a222a8a in qt_fast_timer_proc (timerId=0, user=17105516) at kernel\qeventdispatcher_win.cpp:432
    #16 0x7e4189cd in USER32!GetWindowLongW () from C:\WINDOWS\system32\user32.dll
    #17 0x00000000 in ?? ()
    The QDialog-derived class is generated using Qt Creator and has not been changed by me.

Similar Threads

  1. QDialog.exec() exiting without calling QDialog::accept()
    By doggrant in forum Qt Programming
    Replies: 3
    Last Post: 2nd February 2011, 12:35
  2. QDialog problem with exec()
    By nomadscarecrow in forum Qt Programming
    Replies: 3
    Last Post: 23rd April 2010, 19:40
  3. exec() not blocking, derived QDialog, Qt 4.4.3
    By wdezell in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2009, 19:56
  4. problem with show/exec of Qdialog
    By dudedude in forum Qt Programming
    Replies: 1
    Last Post: 23rd December 2008, 12:20
  5. QDialog: show() and exec() together in constructor?
    By Teuniz in forum Qt Programming
    Replies: 8
    Last Post: 28th February 2007, 12:43

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.