Program with copy-paste when closeEvent with exit(0) cause program crash



  • @joeQ,Thanks!!!
    In my program,I put two QLineEdit in my MainWindow,if I do copy and paste between the two widgets,when I click the "X" and enter the closeEvent where I have the function exit(0) when I accept the quit process,just as these below:

    void MainWindow::closeEvent(QCloseEvent *event)
    {
        QMessageBox warningBox(QMessageBox::Warning,tr("Exit"),("Are you sure to exit?"),QMessageBox::Yes | QMessageBox::No);
        if(QMessageBox::No == warningBox.exec() )
        {
            event->ignore();
        }
        else
        {
            event->accept();
    
            exit(0);
    
            //QCoreApplication::exit(0);
            //QApplication::exit(0);
        }
    }
    

    and the program carshed ,just as below:

    ASSERT: "q != 0" in file kernel\qeventdispatcher_win.cpp, line 524
    ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 130c948. Receiver 'lineEdit' (of type 'QLineEdit') was created in thread 13c9008", file kernel\qcoreapplication.cpp, line 541
    ASSERT: "q != 0" in file kernel\qeventdispatcher_win.cpp, line 524
    ASSERT: "q != 0" in file kernel\qeventdispatcher_win.cpp, line 524
    

    0_1513069960145_picture1.png



  • @vitasr said in QT program with copy-paste when closeEvent with exit(0) cause program crash:

    Receiver 'lineEdit' (of type 'QLineEdit') was created in thread 13c9008

    Are you creating widgets in non-gui threads?

    http://doc.qt.io/qt-5/thread-basics.html#gui-thread-and-worker-thread



  • @vitasr
    I don't know if it's to do with your crash --- which seems to be about different GUI thread --- but why are you explicitly exiting the app in the handler for main window's close event? Don't you just want to let Qt exit the main event loop when it gets the close (after your message box)?

    P.S.
    If you do choose to exit somewhere in your app, for whatever reason, I'm pretty directly sure using exit() is not a good idea. You should at least use one of the Qt-level "quit"s, to allow it a proper chance to clean up, e.g. see https://stackoverflow.com/questions/8026101/correct-way-to-quit-a-qt-program.



  • @vitasr ,

    In close event, close the thread first, then close the application.



  • @vitasr Hi,friend.

    Did you work it out?



  • @VRonin Thanks,all the work I do in the GUI thread,and I find that if I use qCoreQApplication(0),it works well,but if I used exit(0),It crashed.This only happened when I do copy-paste operation in my program,if I donot do copy-paste,exit(0) also works well,so this is the problem



  • @joeQ Thanks!

    Chinese:

    我的英文很不好,所以,和你对话就采用汉语了,抱歉!
    我发现,在closeEvent()函数中如果我调用QCoreApplication(0)取代exit(0)就不会有问题。
    但是:如果我在我的程序中不进行控件之间的赋值黏贴操作,exit(0)也可以正常工作。否则exit(0)就会出现问题。报错原因好像是sendEvent不能跨线程之类的,但我还是不太明白到底是什么原因。

    English:

    In closeEvent(), if i used QCoreApplication::quit(0), it will be Ok. But use exit(0) it crash.

    It also be Ok when i had not any copy and paste operates between widgets that use exit(0).

    The error message like :sendEvent can not cross-thread. what's mean?


  • Qt Champions 2016

    @vitasr said in QT program with copy-paste when closeEvent with exit(0) cause program crash:

    It also be Ok when i had not any copy and paste operates between widgets that use exit(0).

    You're forcefully killing the application and then wonder why it crashes the way down ... just don't use ::exit!



  • @kshegunov
    I suggested that to OP above, but ...


  • Qt Champions 2016

    @JNBarchan said in QT program with copy-paste when closeEvent with exit(0) cause program crash:

    I suggested that to OP above, but ...

    Granted, but you weren't that explicit. (S)he should not use ::exit at all unless handling a SIGSEGV or another signal in a handler.



  • @kshegunov
    I wrote:

    If you do choose to exit somewhere in your app, for whatever reason, I'm pretty directly sure using exit() is not a good idea. You should at least use one of the Qt-level "quit"s ...

    Oh, OK! Next time I'll be more forceful ;-)


Log in to reply
 

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