My program start to crash because of MessageBox



  • Hi everyone! Today i added function:

    void MainWindow::err(QString text)
    {
        QMessageBox::information(this, "ERROR", text);
    }
    

    It worked well, but in some point, my program start to crash when i try to use it, and i don't know what changed it and no info about reason of crash from Qt...

    Please help, i need such error handler back!


  • Lifetime Qt Champion

    Hi
    Are you sure its not crashing from something else?
    Since it used to work, there is no reason it should stop doing so.

    Use the debugger to find the exact line that make it crash.



  • @mrjj said in My program start to crash because of MessageBox:

    Are you sure its not crashing from something else?

    Yes. I removed function where it used, and now no crash. Plus it create that window, with proper title "ERROR", but it's small and no text in it.

    @mrjj said in My program start to crash because of MessageBox:

    Use the debugger to find the exact line that make it crash.

    how would i do that same simple and with one line(never used debuggers before).



  • @Engelard
    No reason it would crash, unless MainWindow deleted while it's shown, which it won't be from the code you show.

    Plus it create that window, with proper title "ERROR", but it's small and no text in it.

    That is wrong. Create a standalone program which does nothing other than display this message box and test.



  • @JonB said in My program start to crash because of MessageBox:

    That is wrong. Create a standalone program which does nothing other than display this message box and test.

    I told already that it worked like half day ago, now it's crashed, exactly when i call this function, i did nothing global or related to this function in my program.


  • Qt Champions 2018

    @Engelard said in My program start to crash because of MessageBox:

    i did nothing global or related to this function in my program.

    But something has changed - and since Qt don't crash just because of a simple QMessageBox call and you're unable to provide a proper testcase or backtrace you're on your own tracking down the problem by e.g. removing stuff until the crash never occurs.



  • @Engelard
    Then how can anybody possibly know what you might have done to make something which should be able to display some text without problem now not be able to display it, before we ever get as far as crashing?

    Either use a debugger, or chop out the code you added till it works again/build up from a standalone which does work. If you think anyone here is going to be able to tell you why your unknown code "suddenly" does not work, they will not.



  • @Christian-Ehrlicher said in My program start to crash because of MessageBox:

    and you're unable to provide a proper testcase or backtrace you're on your own tracking down the problem

    Exactly, i can't backtrace it because there is nothing to trace rly, just a small function...

    @JonB said in My program start to crash because of MessageBox:

    Then how can anybody possibly know what you might have done to make something

    Well, maybe it is some another kind of Qt bug, which someone might know how to fix.

    It is not about my function, it is about message box itself, i get same crashes if try use simple static functions like QMessageBox::information();



  • @Engelard

    Exactly, i can't backtrace it because there is nothing to trace rly, just a small function...

    Yes, you can, and should. Run the program under the Qt Creator debugger. Assuming it really does "crash", at that point the debugger should catch it. Go to the "stack trace" (or "back trace") window at that point, and paste here whatever information it does have.

    BTW, you can probably also try

    QMessageBox::information(nullptr, "ERROR", text);
    

    and see whether that too "crashes" or not.


  • Lifetime Qt Champion

    Hi
    Just start the app in debug mode. And then make it crash.
    It should show in call stack what it was doing before it died.

    Also, just for test add a new button and call
    err("TEST");
    to see if that triggers the crash.
    If not, it must have something to do with the surrounding code from where you
    use err() . maybe a dangling pointer.


  • Moderators

    @Engelard what os and qt version is this?

    I had some issues like this under ios and 5.12.0 went away with one of the later updates

    also do you call processEvents() somewhre inside your code?
    the static call to QMessageBox spins its own QEventLoop. That on combination with processEvent calls can cause very strange behaviour and crashes.



  • @J.Hilk

    QMessageBox::information();
    

    just as that statement, blocks, spins its own loop, and does not return till the user has pressed a button, correct or not? So you can't be calling processEvents() elsewhere in your own code, unless you're doing something really odd, is that not right?


  • Qt Champions 2018

    @JonB said in My program start to crash because of MessageBox:

    is that not right

    When there is a timer which is executed while the message box is shown and in the slot you call processEvents() you will get likely into trouble.


  • Moderators

    @JonB Just because you call start on an other event loop doesn't mean your original one simply stops.

    You get a general setDisbaled on your MainApplication Ui, everything else is still going on normally.

    Take this example, based on what @Christian-Ehrlicher said:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QTimer alive;
        QObject::connect(&alive, &QTimer::timeout, []()->void{qDebug("Alive");});
        alive.start(500);
    
        QPushButton btn;
        btn.show();
        QObject::connect(&btn, &QPushButton::clicked, []()->void{
                             QMessageBox::information(nullptr, QString("Error"), QString("Some Error Text"));
                         });
    
        return a.exec();
    }
    


  • @Christian-Ehrlicher , @J-Hilk
    Yep, fair enough, thank you. I was not thinking about a timer. And nor about a timer calling processEvents(), which I would think would not be advisable!

    We shall see whether the OP's problem stems from this.



  • @mrjj said in My program start to crash because of MessageBox:

    Just start the app in debug mode. And then make it crash.
    It should show in call stack what it was doing before it died.

    It shows numerous windows like this:

    0_1553493942688_1111.jpg

    What is all mean?

    0_1553493971315_mmm.jpg

    @J.Hilk said in My program start to crash because of MessageBox:

    also do you call processEvents() somewhre inside your code?

    No


  • Qt Champions 2018

    @Engelard said in My program start to crash because of MessageBox:

    It shows numerous windows like this:

    These are uninteresting.

    We need the call stack (also called stack trace, or backtrace)


  • Lifetime Qt Champion

    Hi
    the call stack / trace it looks like this
    alt text



  • 0_1553503901234_111.jpg
    0_1553503907073_222.jpg
    0_1553503911733_333.jpg


  • Lifetime Qt Champion

    @Engelard
    and that is from it crashes ?



  • @mrjj If i comment out my line

    QMessageBox::information(nullptr, "ERROR", "trtrtr");
    

    Debugger will be absolutely empty. When i use this static function of messBox, it give me such output.



  • Hey i just found that it work well if i put it in the constructor, so it crashes only when it in my other functions, strange.

    UPDATE:

    Here is info for everyone. Yesterday, i noticed that the app not just crashed, it crashed like.. twice, and all my app mostly about the use of "pressed" and "released" SLOTs of the buttons. I removed usage of release slot, and now app do just single crash. Here is how "release button" chain looks like:

    void MainWindow::on_button_Q_released()
    {
        //lightRestore(currentButton);
    }
    

    And here is that only function which it use:

    void MainWindow::lightRestore(QPushButton *lamp)
    {
        lamp->setStyleSheet("QPushButton{color: white;background-color: rgb(35,35,35);border-style: outset;border-width: 2px;border-radius: 15px;border-color:  black;font: bold 12px;}");
    }
    


  • Hi, if I read the trace correctly, the user presses the button_G, that functions calls mainJourney() which issues the QMessageBox() call.
    Could you show the code for mainJourney()?


  • Moderators

    @Engelard said in My program start to crash because of MessageBox:

    currentButton

    seems like currentButton is no longer a valid QPushButton-Pointer,

    If you have a pointer member variable, you should make sure that you always set it to a nullptr when the pointed to object is destroyed/invalid.
    And always check against that before accessing it:

    void MainWindow::lightRestore(QPushButton *lamp)
    {
        if(lamp)
            lamp->setStyleSheet(...);
    }
    


  • @hskoglund said in My program start to crash because of MessageBox:

    Could you show the code for mainJourney()?

    No, that stuff is too big, it called from pressButton SLOT, above much simplier and compact example of release, it does the same crash.



  • @J.Hilk nice advice, tnx. But it valid(without that messageBox stuff everything works perfectly), i added if-check line. Nothing it changed(2nd crash exist).

    UPDATE:

    If i put messageBox AFTER assignment of that button - will be no crashes, but also it is not possible to hold the button(because this messageBox take over priority and it is now main waindow).

    void MainWindow::on_button_Q_pressed()
    {
        currentButton = mainJourney('Q');
        lightSet(currentButton);
        QMessageBox::information(nullptr, "ERROR", "trtrtr");
    }
    


  • Oh, finally! With the help of @J-Hilk i realised what was wrong.

    1. add if-cheks in "lights" functions.
    2. assign currentButton to nullptr in the constructor. (i mistakenly thought that created pointers of the objects without assignment by default nullptr)

    Now no crashes. Tnx @J-Hilk !


Log in to reply
 

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