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. The Qt App breaks down during deconstruction of MainWin
Forum Updated to NodeBB v4.3 + New Features

The Qt App breaks down during deconstruction of MainWin

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 3.6k Views 3 Watching
  • 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.
  • R Offline
    R Offline
    Rapidrain
    wrote on last edited by kshegunov
    #1

    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]

    kshegunovK 1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      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;
      }
      
      mrjjM R 2 Replies Last reply
      2
      • M mpergand

        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;
        }
        
        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        adding to @mpergand
        please see here
        http://doc.qt.io/qt-5/objecttrees.html

        1 Reply Last reply
        1
        • R Rapidrain

          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]

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

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

          Read and abide by the Qt Code of Conduct

          R 1 Reply Last reply
          0
          • kshegunovK kshegunov

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

            R Offline
            R Offline
            Rapidrain
            wrote on last edited by kshegunov
            #5

            @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]

            kshegunovK 1 Reply Last reply
            0
            • M mpergand

              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;
              }
              
              R Offline
              R Offline
              Rapidrain
              wrote on last edited by Rapidrain
              #6

              @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?

              mrjjM 1 Reply Last reply
              0
              • R Rapidrain

                @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?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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

                1 Reply Last reply
                1
                • R Rapidrain

                  @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]

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @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.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  2
                  • R Offline
                    R Offline
                    Rapidrain
                    wrote on last edited by
                    #9

                    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.

                    mrjjM 1 Reply Last reply
                    0
                    • R Rapidrain

                      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.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @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

                      R 1 Reply Last reply
                      2
                      • mrjjM mrjj

                        @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

                        R Offline
                        R Offline
                        Rapidrain
                        wrote on last edited by
                        #11

                        @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?

                        EddyE mrjjM 2 Replies Last reply
                        0
                        • R Rapidrain

                          @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?

                          EddyE Offline
                          EddyE Offline
                          Eddy
                          wrote on last edited by
                          #12

                          @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 Certified Specialist
                          www.edalsolutions.be

                          1 Reply Last reply
                          1
                          • R Rapidrain

                            @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?

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #13

                            @Rapidrain

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

                            Well its a symbol/icon for tag.

                            It inserts the back quotes.

                            1 Reply Last reply
                            1

                            • Login

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