Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Project crashes when closing Mainwindow
QtWS25 Last Chance

Project crashes when closing Mainwindow

Scheduled Pinned Locked Moved Solved General and Desktop
crashingpointerdynamic allocat
5 Posts 3 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Another Qt Beginner
    wrote on 29 Dec 2019, 08:29 last edited by
    #1

    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:
    Breakpoint1.png
    Breakpoint 2:
    Breakpoint2.png
    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
    '

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 29 Dec 2019, 08:35 last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      6
      • D Offline
        D Offline
        dheerendra
        Qt Champions 2022
        wrote on 29 Dec 2019, 10:31 last edited by
        #3

        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.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        3
        • A Offline
          A Offline
          Another Qt Beginner
          wrote on 29 Dec 2019, 20:19 last edited by
          #4

          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!

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 29 Dec 2019, 22:03 last edited by
            #5

            Which one do you mean ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            4/5

            29 Dec 2019, 20:19

            • Login

            • Login or register to search.
            4 out of 5
            • First post
              4/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved