Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

double free or corruption (out) why



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


  • Lifetime Qt Champion

    Please show us the backtrace of the crash.



  • @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)
    


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



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



  • 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!


  • Lifetime Qt Champion

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


Log in to reply