The Qt App breaks down during deconstruction of MainWin



  • I've got a program with a QMainWindow holding a QWidget inside where a pushbutton and a QRect are painted.

    I've reduced the problem to a simple example. When I have the rectangle and pushbutton all constructed and visible, I click on the exit QPushButton and the app will not come to a complete close. The app stops at the end of the MainWin deconstructor and freezes there with a remark about heap and lots of hexadecimal.

    Am I using the struct incorrectly? It appears to be an undeleted pointer but what??

    //----------------MainWin.h----------------------------
    #include <QMainWindow>
    #include <QResizeEvent>
    
    #include "QWid1.h"
    
    class MainWin : public QMainWindow {
    
    	Q_OBJECT
    	QWid1* qwid1;
    
    public:
    	MainWin(QWidget* parent = 0);
    	~MainWin();
    
    	void resizeFrame();
    
    protected:
    	void resizeEvent(QResizeEvent*);
    
    public slots:
    	void pbExit_Slot();
    };
    
    #endif /* SRC_PANELS_MAINWIN_H_ */
    
    //----------------MainWin.cpp----------------------------
    MainWin::MainWin(QWidget* parent) : QMainWindow(parent) {
    
    	qwid1 = new QWid1(this);
    	qwid1->setGeometry(0, 0, 800, 500);
    
    	resizeFrame();
    }
    
    MainWin::~MainWin() {
    
    	delete qwid1;
    }
    
    //----------------QWid1.h----------------------------
    struct MyBox {
    
    	QRect rect;
    };
    
    class QWid1 : public QWidget {
    
    	Q_OBJECT
    
    	QPushButton* button;
    	MyBox myBox;
    
    public:
    	QWid1(QWidget* parent = 0);
    	~QWid1();
    
    protected:
    	void paintEvent(QPaintEvent*);
    };
    
    #endif /* QWID1_H_ */
    
    //----------------QWid1.cpp----------------------------
    QWid1::QWid1(QWidget* parent) : QWidget(parent) {
    
    	MainWin* mainwin = (MainWin*)parent;
    
    //      create the black rectangle
    	myBox.rect.setRect(0, 0, 100, 100);
    
    //      create the exit button
    	button = new QPushButton(this);
    	button->setGeometry(100, 200, 100, 40);
    	button->setText("Exit");
    
    //      connect the exit button click event to a MainWin slot
    	connect(button, &QPushButton::clicked, mainwin, &MainWin::pbExit_Slot);
    
    //	paint the background white
    	QPalette palette;
    	QBrush brush1(Qt::white);
    	brush1.setStyle(Qt::SolidPattern);
    	palette.setBrush(QPalette::Active, QPalette::Window, brush1);
    
    	setPalette(palette);
    	setAutoFillBackground(true);
    }
    
    QWid1::~QWid1() {
    
    	delete button;
    }
    
    void QWid1::paintEvent(QPaintEvent*) {
    
    	QPainter painter(this);
    
    //	paint the rectangle black
    	QPen pen1 = QPen(QColor(50, 50, 50));
    	pen1.setWidth(2);
    	painter.setPen(pen1);
    
    	painter.drawRect(myBox.rect);
    }
    

    error output on Eclipse IDE (Neon.3) : I have a jpg, but cannot upload it.

    [Added code tags ~kshegunov]



  • qwid1 is deleted two times !

    MainWin::MainWin(QWidget* parent) : QMainWindow(parent) {
    
    qwid1 = new QWid1(this);  -----> deleted by MainWindow
    qwid1->setGeometry(0, 0, 800, 500);
    
    resizeFrame();
    }
    
    MainWin::~MainWin() {
    
    delete qwid1;  ----> no need to delete it, already made by MainWindow
    }
    

    Same error here:

    QWid1::~QWid1() {
    
    delete button;
    }
    

  • Qt Champions 2016


  • Qt Champions 2016

    What do you have in pbExit_Slot and what's the stack trace from the crash?



  • @kshegunov what is a stack trace?

    A list of the routines called at the time it freezes??
    I have a jpg of a screen shot but how do I put a jpg onto this ??

    in pbExit_Slot() :

    void MainWin::pbExit_Slot() {
    
    	try {
    
    		close();
    	}
    	catch(QException e) {
    		std::cout << "Exception error in MainWin::pbExit_Slot()" << std::endl;
    	}
    } // void MainWin::pbExit_Slot()
    

    [Added code tags ~kshegunov]



  • @mpergand qwid1 is a QWid1 object, button is a QPushButton object.....two different things
    I see delete qwid1 deleted one single time, in the destructor of MainWin
    Why would MainWin delete a member without writing "delete qwid1" ?

    P.S. what are code tags?


  • Qt Champions 2016

    @Rapidrain
    If you assign an owner to a widget, the owner will delete it.
    Please read this
    http://doc.qt.io/qt-5/objecttrees.html


  • Qt Champions 2016

    @Rapidrain said in The Qt App breaks down during deconstruction of MainWin:

    what is a stack trace?

    A list of the function calls (since they are put in the stack, hence the name). See here for a general discussion. Are you running your program through the debugger, if not, you should. Whenever the crash occurs the call stack will be visible in the "stack" window of the debugger view.

    I have a jpg of a screen shot but how do I put a jpg onto this ??

    Upload it somewhere (e.g. postimage.org) and then use ![](http:site.com/image/url) in your post to show it.

    P.S. what are code tags?

    Before posting any code insert ``` (three back quotes) on their own line, then add your code, and finish with another three backquotes to finish the code block, this will format it in a readable fashion.



  • mrjj had the answer in that link to a qt doc page
    Wow.
    I've worked and doodled with Qt for almost 10 years and I had never heard of this self-deletion of objects on the heap.
    Never had a problem until now.
    As with the code tags, learning by making mistakes here, and your odd manner of uploading jpgs, also learning by making mistakes, I had to make the mistake with this app to find out about self-deletion.

    Thanks to mrjj.


  • Qt Champions 2016

    @Rapidrain
    Np.
    The owner system is pretty handy as you do not have to clean up manually and it help reduce
    the change of leaks.

    Regarding the code tags. Its the last button
    alt text



  • @mrjj
    Thanks mrjj. Someone here said I should use three "back quotes".
    </> does not look like a back quote to me.
    And hovering over the symbol shows no mini-text explanation.
    Are explanations expensive?


  • Moderators

    @Rapidrain said in The Qt App breaks down during deconstruction of MainWin:

    And hovering over the symbol shows no mini-text explanation.
    Are explanations expensive?

    Hi Rapidrain,

    That's a known issue that has been reported already. It should be in the next Forum update, which is in testing fase now.

    Explanations are not expensive since we are all volunteers here trying to help each other with Qt. ;-)
    If you want to dive deeper in the editor here are some links :
    wiki page on editor
    link to cheatsheets

    Feel free to add more information to the wiki page if you think it's not good enough.

    Eddy


  • Qt Champions 2016

    @Rapidrain

    • </> does not look like a back quote to me.

    Well its a symbol/icon for tag.

    It inserts the back quotes.


Log in to reply
 

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