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. double free or corruption (out) why
Forum Updated to NodeBB v4.3 + New Features

double free or corruption (out) why

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 2.0k Views 2 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.
  • pauleddP Offline
    pauleddP Offline
    pauledd
    wrote on last edited by pauledd
    #1

    Hi

    I have a mainwindow derived from a QWidget. Then I've a second class called RegWindow that opens if I click a QPUshbutton in the Mainwindow.

    mainwindow.h:

    class MainWindow : public QWidget
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = nullptr);
    private:
    ...
        RegWindow *regWindow;
        QPushButton *pb;
    ...
    public slots:
        void openRegWindow();
    };
    

    mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent)
        :QWidget(parent)
    {
        pb = new QPushButton("START");
        regWindow = new RegWindow(this);
    
        connect(pb, &QPushButton::clicked, this, &MainWindow::openRegWindow);
    
    }
    void MainWindow::openRegWindow()
    {
        regWindow->show();
    }
    

    In the RegWindow I create a bunch of Labels and Checkboxes in a for loop in a QVector and put it in a GridLayout.
    But then I want to create a QLabel and the application crashes:
    regwindow.h:

    class RegWindow : public QDialog
    {
        Q_OBJECT
    public:
        explicit RegWindow(QWidget *parent = nullptr);
    
    private:
        QGridLayout *layGrid;
        QVector<QLabel*> lab;
        QVector<QCheckBox*> check;
        QLabel *label;
    };
    

    regwindow.cpp:

    RegWindow::RegWindow(QWidget *parent)
        :QDialog(parent)
    {
        layGrid = new QGridLayout;
        for(int i=0;i<24;i++){
            lab.append(new QLabel(QString::number(23-i)));
            check.append(new QCheckBox);
            layGrid->addWidget(lab.at(i),0,i);
            layGrid->addWidget(check.at(i),1,i);
        }
        label = new QLabel("Test");
        layGrid->addWidget(label,2,1);
        this->setLayout(layGrid);
    }
    

    I get error:

    double free or corruption (out)
    

    BUT if I create the QLabel in the regwindow.cpp like so:

    QLabel *label = new QLabel("test");
    

    No errors at all.
    Whats the matter here?

    Pablo J. RoginaP 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Please show us the backtrace of the crash.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      pauleddP 1 Reply Last reply
      0
      • pauleddP pauledd

        Hi

        I have a mainwindow derived from a QWidget. Then I've a second class called RegWindow that opens if I click a QPUshbutton in the Mainwindow.

        mainwindow.h:

        class MainWindow : public QWidget
        {
            Q_OBJECT
        public:
            explicit MainWindow(QWidget *parent = nullptr);
        private:
        ...
            RegWindow *regWindow;
            QPushButton *pb;
        ...
        public slots:
            void openRegWindow();
        };
        

        mainwindow.cpp

        MainWindow::MainWindow(QWidget *parent)
            :QWidget(parent)
        {
            pb = new QPushButton("START");
            regWindow = new RegWindow(this);
        
            connect(pb, &QPushButton::clicked, this, &MainWindow::openRegWindow);
        
        }
        void MainWindow::openRegWindow()
        {
            regWindow->show();
        }
        

        In the RegWindow I create a bunch of Labels and Checkboxes in a for loop in a QVector and put it in a GridLayout.
        But then I want to create a QLabel and the application crashes:
        regwindow.h:

        class RegWindow : public QDialog
        {
            Q_OBJECT
        public:
            explicit RegWindow(QWidget *parent = nullptr);
        
        private:
            QGridLayout *layGrid;
            QVector<QLabel*> lab;
            QVector<QCheckBox*> check;
            QLabel *label;
        };
        

        regwindow.cpp:

        RegWindow::RegWindow(QWidget *parent)
            :QDialog(parent)
        {
            layGrid = new QGridLayout;
            for(int i=0;i<24;i++){
                lab.append(new QLabel(QString::number(23-i)));
                check.append(new QCheckBox);
                layGrid->addWidget(lab.at(i),0,i);
                layGrid->addWidget(check.at(i),1,i);
            }
            label = new QLabel("Test");
            layGrid->addWidget(label,2,1);
            this->setLayout(layGrid);
        }
        

        I get error:

        double free or corruption (out)
        

        BUT if I create the QLabel in the regwindow.cpp like so:

        QLabel *label = new QLabel("test");
        

        No errors at all.
        Whats the matter here?

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @pauledd one related thing at first glance looking at your code snippets is that you're avoiding the great feature provided by Qt which is "object parenting", or implicit memory management. From Qt documentation:

        Objects organize themselves in object trees. When you create a QObject with another object as parent, the object will automatically add itself to the parent's children() list. The parent takes ownership of the object; i.e., it will automatically delete its children in its destructor. You can look for an object by name and optionally type using findChild() or findChildren().

        I'm not sure why you did ths (which is OK)

        regWindow = new RegWindow(this);
        

        but then you don't keep with parenting:

            layGrid = new QGridLayout;
        ...
        new QLabel(QString::number(23-i))
        ...
        label = new QLabel("Test");
        ...
        new QCheckBox
        

        so at least those object creation lines could be:

            layGrid = new QGridLayout(this);
        ...
        new QLabel(QString::number(23-i), this)
        ...
        label = new QLabel("Test", this);
        ...
        new QCheckBox(this)
        

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          Please show us the backtrace of the crash.

          pauleddP Offline
          pauleddP Offline
          pauledd
          wrote on last edited by
          #4

          @Christian-Ehrlicher said in double free or corruption (out) why:

          Please show us the backtrace of the crash.

          yes I will tomorrow.

          @Pablo-J-Rogina
          Doesn't the

          this->setLayout(layGrid);
          

          reparent everything to the mainwindow whats inside the Gridlayout?

          M 1 Reply Last reply
          0
          • pauleddP pauledd

            @Christian-Ehrlicher said in double free or corruption (out) why:

            Please show us the backtrace of the crash.

            yes I will tomorrow.

            @Pablo-J-Rogina
            Doesn't the

            this->setLayout(layGrid);
            

            reparent everything to the mainwindow whats inside the Gridlayout?

            M Offline
            M Offline
            mpergand
            wrote on last edited by
            #5

            Doesn't the

            this->setLayout(layGrid);
            

            reparent everything to the mainwindow whats inside the Gridlayout?

            Yes it is.
            But not here:
            pb = new QPushButton("START");

            It doesn't explain the crash anyway.

            Run your app in debug mode and look at the debugger stack panel, it should be informative about what goes wrong.

            1 Reply Last reply
            1
            • pauleddP Offline
              pauleddP Offline
              pauledd
              wrote on last edited by
              #6

              ok, so concerning the pushbutton in the mainwindow, it is actually added to another gridlayout... but of cause you couldnt have known that because I withheld the complete code...

              Then for the backtrace another thing you didnt know. I crosscompile for the raspberry pi and deploy the application to the raspi. Because I use the raspi-tools toolchain Qtcreator refused to start the toolchain gdb version because it was lacking something python related. Alternativley I tried to debug the app directly on the raspi with weird output so I gave up for that.

              This morning Instead I tried to create minimal reproducible example and I realized that the code runs now without any errors... I really dont know what happend but my guess is that there was something wrong with either qqtcreator or my raspberry or both. Yesterday I also noticed that in the QDialog class Qtcreator was not able to autocomplete the "Q_OBJECT" macro in that class, it simply didnt show up in the dropdown list but if I wrote out Q_OBJECT it changed color and seemed to be recognized corretly... that drove me crazy. Today I can select it from the list.

              To cut a long story short, after restarting the Raspi and my DesktopPc everything works now, unfortunately without knowing the reason for the yesterday failure...

              Thanks for you help!

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @pauledd: Nice to hear, can you please mark the topic as solved, thx :)

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                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