Crashed at QCoreApplication::sendSpontaneousEvent



  • 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



  • 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;
    };
    @



  • Try
    @
    mEdit->deleteLater();
    @



  • @Gerolf
    Thanks.
    deleteLater worked :D


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.