Why my programme crashed when I enter from a dialog to a mainwindow
-
this is my slot function
void Dialog::on_pushButton_login_clicked() { UserMainWindow* userWindow = new UserMainWindow(nullptr); userWindow->show(); // this->hide(); delete this; }
-
I found the cause of problem, because I created a object in stack in main(), not in heap. Apparently, I delete it once, and then the object destructor deleted it again, so the programme crased due to two deletions.
int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog* dlg = new Dialog; dlg->show(); return a.exec(); }
-
@Wgxzzzzzzc said in Why my programme crashed when I enter from a dialog to a mainwindow:
delete this;
I wouldn't delete
this
while I am inside a slot onthis
, and showing the dialog!?Close the dialog properly (e.g.
QDialog::done()
), and probably useQt::WA_DeleteOnClose
flag on the dialog. -
@JonB said in Why my programme crashed when I enter from a dialog to a mainwindow:
inside
Yea, I know my code is wrong but I just want to know the cause of crash. I use your solution but it crashed too.
-
@Wgxzzzzzzc said in Why my programme crashed when I enter from a dialog to a mainwindow:
but it crashed too.
As always when a crash occurs - use a debugger to see where and why it crashes.
-
@Wgxzzzzzzc You should never delete an instance of a class while it is inside of one of its non-static methods! If you do so, "this" pointer becomes a dangling pointer and if it is then dereferenced your application crashes.
-
@Wgxzzzzzzc
As @Christian-Ehrlicher says, use a debugger for the stack trace when it crashes.but even if you use my
(e.g.
QDialog::done()
), and probably useQt::WA_DeleteOnClose
flag(or your
delete
) if you then try to access yourDialog
instance after that it no longer exists and will crash, is that your situation? -
but as you see, I don't use "this" after i delete "this". the instance of UserMainWindow pass a nullptr as its parent.
-
@Wgxzzzzzzc
No, I do not see that. I see what you do from inside your slot, but not what you may or may not do with theDialog
instance anywhere else.In any case, as already said if you want to find out why it crashes run it from debugger. Anything else is guesswork.
-
I found the cause of problem, because I created a object in stack in main(), not in heap. Apparently, I delete it once, and then the object destructor deleted it again, so the programme crased due to two deletions.
int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog* dlg = new Dialog; dlg->show(); return a.exec(); }
-
@Wgxzzzzzzc Previous error code.
int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog dlg; dlg.show(); return a.exec(); }