Project crashes when closing Mainwindow
-
Hi,
I've just started my first QtProject. And everytings fine, but I get the message: 'The program has unexpectedly finished.'.
I already have a clue, it might have something to do with the way I handle the pointers / dynamic memory [I guess I am doing something horrible to my memory here;) ].My main.cpp:
#include "mainwindow.h" #include <QApplication> #include "QHBoxLayout" int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
the mainwindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include "nodeview.h" #include "qgraphicsscene.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; // Declaration NodeView *view; // Example Scene QGraphicsScene *scene1; QGraphicsSimpleTextItem *text; QGraphicsEllipseItem *ellipse; }; #endif // MAINWINDOW_H
the mainwindow .cpp:
#include "mainwindow.h" #include "ui_mainwindow.h" #include "QGraphicsSimpleTextItem" #include "QGraphicsEllipseItem" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); setWindowTitle("Nodes"); // create NodeView view = new NodeView(); setCentralWidget(view); // Breakpoint 1 // create Example Scene scene1 = new QGraphicsScene(); // Breakpoint 2 // Scene Content text = new QGraphicsSimpleTextItem("this is a simple text"); text->setFont(QFont("ubuntu", 24)); text->setBrush(QBrush(Qt::red)); text->setPos(view->mapToScene(-1000, -100)); scene1->addItem(text); ellipse = new QGraphicsEllipseItem(-100, -100, 200, 200); scene1->addItem(ellipse); view->setScene(scene1); } MainWindow::~MainWindow() { delete ui; delete view; delete scene1; delete text; delete ellipse; }
I tried debugging, but I've honestly never really done that and I don't know what I am doing. But I came up with this to breakpoints and got those two results:
Breakpoint 1:
Breakpoint 2:
The scene1 has this weird red '0x6c' even before the parent constructor call and the Type changes at initalizating from '*QGraphicsScene **' to 'QGraphicsScene'.Any clues what's happening here?
Thanks so much!
PS: I can deal with usual compiler errors but those memory stuff is still magic to me ;)PPS: the debug crashes, too, at some point:
'The inferior stopped because it received a signal from the operating system.
Signal name : SIGSEGV
Signal meaning : Segmentation fault' -
Hi and welcome to devnet,
Please take a read on the Qt object tree and parent-child relationship.
You are currently triggering a double deletion. Objects with proper parenting will be automatically deleted.
Note that not all classes starting with Q are QObjects and not everything gets reparented automatically. The documentation is explicit about that so do not hesitate to check it.
-
App must be crashing when you close the application. As @SGaist already pointed it is happening due to deleting the object twice. Just comment the code in destructor and see how it behaves. I'm sure nothing crashes.
After this delete only ui object in destructor & see how it goes. -
Hi,
first of all: thank you so much for help!I started with removing all delete's and it worked, as in 'Object Tree' described.
I played around a little and it actually even works with some of these lines still enabled, in some random combinations.
PS: Only because I am curious: does the different behavior from QGraphicsScene in the memory described erlier mean anything / is there a reason for that, or is it just random behavior?
Thanks anyways!
-
Which one do you mean ?