Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Crashed at QCoreApplication::sendSpontaneousEvent
Forum Updated to NodeBB v4.3 + New Features

Crashed at QCoreApplication::sendSpontaneousEvent

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 5.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fifth
    wrote on last edited by
    #1

    I built a quick demo as following,

    @
    class MainView : public QMainWindow
    {
    Q_OBJECT

    public:
    MainView()
    {
    mMsgBox = new QMessageBox(this);
    }

    ~MainView()
    {
        delete mEdit;
        delete mMsgBox;
    }
    
    void build()
    {
        mEdit = new QTextEdit(this);
        bool ret = connect(mEdit, SIGNAL(textChanged()), this, SLOT(showMsgBox()));
        ret = false;
    }
    

    public slots:
    void showMsgBox()
    {
    mMsgBox->setText("Click to delete edit");
    bool ret = connect(mMsgBox, SIGNAL(finished(int)), this, SLOT(deleteEdit(int)));
    mMsgBox->exec();
    }

    void deleteEdit(int)
    {
        delete mEdit;
        mEdit = NULL;
    }
    

    private:
    QTextEdit* mEdit;
    QMessageBox* mMsgBox;
    };
    @

    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,

    bq.
    // 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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Hi,

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

      in the constructor, you don't initialize all variables (mEdit!)

      where do you call build?

      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:

      @
      class MainView : public QMainWindow
      {
      Q_OBJECT

      public:
      MainView()
      {
      mEdit = 0;
      mMsgBox = new QMessageBox(this);
      }

      // ...
      

      public slots:
      // ...

      void deleteEdit(int)
      {
          mEdit->deleteLater();
      }
      

      private:
      QPointer<QTextEdit> mEdit;
      QPointer<QMessageBox> mMsgBox;
      };
      @

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

      1 Reply Last reply
      0
      • L Offline
        L Offline
        loladiro
        wrote on last edited by
        #3

        Try
        @
        mEdit->deleteLater();
        @

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fifth
          wrote on last edited by
          #4

          @Gerolf
          Thanks.
          deleteLater worked :D

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved