June 21, 2011

fifth fifth
Lab Rat
57 posts

Crashed at QCoreApplication::sendSpontaneousEvent

 

I built a quick demo as following,

  1. class MainView : public QMainWindow
  2. {
  3.     Q_OBJECT
  4.  
  5. public:
  6.     MainView()
  7.     {
  8.         mMsgBox = new QMessageBox(this);
  9.     }
  10.  
  11.     ~MainView()
  12.     {
  13.         delete mEdit;
  14.         delete mMsgBox;
  15.     }
  16.  
  17.     void build()
  18.     {
  19.         mEdit = new QTextEdit(this);
  20.         bool ret = connect(mEdit, SIGNAL(textChanged()), this, SLOT(showMsgBox()));
  21.         ret = false;
  22.     }
  23.  
  24. public slots:
  25.     void showMsgBox()
  26.     {
  27.         mMsgBox->setText("Click to delete edit");
  28.         bool ret = connect(mMsgBox, SIGNAL(finished(int)), this, SLOT(deleteEdit(int)));
  29.         mMsgBox->exec();
  30.     }
  31.  
  32.     void deleteEdit(int)
  33.     {
  34.         delete mEdit;
  35.         mEdit = NULL;
  36.     }
  37.  
  38. private:
  39.     QTextEdit* mEdit;
  40.     QMessageBox* mMsgBox;
  41. };

The above demo crashed at QCoreApplication::sendSpontaneousEvent, since event was still sent to deleted edit.
I knew the usage was sort of weird but I got it anyway.

I guessed it had something to do with modal dialog, since I found there was a comment at QApplicationPrivate::sendMouseEvent,

// We need this quard in case someone opens a modal dialog / popup. If that’s the case
// leaveAfterRelease is set to null, but we shall not update lastMouseReceiver.

How to prevent it from crash?
Thanks in advance

3 replies

June 21, 2011

Gerolf Gerolf
Hobby Entomologist
3287 posts

Hi,

form my point of view, there are some errors in your code:

  1. in the constructor, you don’t initialize all variables (mEdit!)
  2. where do you call build?
  3. you are deleting an object inside it’s signal —> that’s not allowed.

you edit is emitting a signal. as it is inside the same thread, it’s direct call —> direct function call of showMsgBox(). inside show message box, which is inside keyPressEvent Handler of the line edit, you call deleteEdit(int) which deletes the object. Then you return in the call stack and boom, you reach a section that does not exist anymore.

you can solve this by using:

  1. class MainView : public QMainWindow
  2. {
  3.     Q_OBJECT
  4.  
  5. public:
  6.     MainView()
  7.     {
  8.         mEdit = 0;
  9.         mMsgBox = new QMessageBox(this);
  10.     }
  11.  
  12.     // ...
  13.  
  14. public slots:
  15.     // ...
  16.  
  17.     void deleteEdit(int)
  18.     {
  19.         mEdit->deleteLater();
  20.     }
  21.  
  22. private:
  23.     QPointer<QTextEdit> mEdit;
  24.     QPointer<QMessageBox> mMsgBox;
  25. };

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

June 21, 2011

loladiro loladiro
Lab Rat
596 posts

Try

  1. mEdit->deleteLater();

June 22, 2011

fifth fifth
Lab Rat
57 posts

@Gerolf
Thanks.
deleteLater worked :D

 
  ‹‹ Dock a QDockWidget in a QToolbar      [SOLVED] how to parse character pointer; ››

You must log in to post a reply. Not a member yet? Register here!