Solved 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!
-
@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(...); }
-
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, unlessMainWindow
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.
-
@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();
-
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.
-
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. -
@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. -
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? -
@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.
-
@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 callingprocessEvents()
, 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:
What is all mean?
@J.Hilk said in My program start to crash because of MessageBox:
also do you call processEvents() somewhre inside your code?
No
-
@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)
-
Hi
the call stack / trace it looks like this
-
-
@Engelard
and that is from it crashes ?