Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. double free or corruption (out):
QtWS25 Last Chance

double free or corruption (out):

Scheduled Pinned Locked Moved Solved The Lounge
qt5.5pointers
5 Posts 2 Posters 9.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.
  • C Offline
    C Offline
    ChajusSaib
    wrote on 27 Oct 2015, 21:03 last edited by
    #1

    Hello lads, so I've just started reading C++ GUI Programming with Qt4.2 2nd edition and in in chapter 2 a bit of the the constructor is like this:

     QHBoxLayout *topLeftLayout = new QHBoxLayout;
     topLeftLayout->addWidget(label);
     topLeftLayout->addWidget(lineEdit);
    
     QVBoxLayout *leftLayout = new QVBoxLayout;
     leftLayout->addLayout(topLeftLayout);
     leftLayout->addWidget(caseCheckBox);
     leftLayout->addWidget(backwardCheckBox);
    
     QVBoxLayout *rightLayout = new QVBoxLayout;
     rightLayout->addWidget(findButton);
     rightLayout->addWidget(closeButton);
     rightLayout->addStretch();
    
     QHBoxLayout *mainLayout = new QHBoxLayout;
     mainLayout->addLayout(leftLayout);
     mainLayout->addLayout(rightLayout);
     setLayout(mainLayout);
    

    This gives me no errors and works but the problem but I do not understand why, how does all this data located get freed?
    I try run this: (notice that I do no dynamically locate the objects)

     QHBoxLayout topLeftLayout;
     topLeftLayout.addWidget(label);
     topLeftLayout.addWidget(lineEdit);
    
     QVBoxLayout leftLayout;
     leftLayout.addLayout(&topLeftLayout);
     leftLayout.addWidget(caseCheckBox);
     leftLayout.addWidget(backwardCheckBox);
    
     QVBoxLayout rightLayout;
     rightLayout.addWidget(findButton);
     rightLayout.addWidget(closeButton);
     rightLayout.addStretch();
    
     QHBoxLayout mainLayout;
     mainLayout.addLayout(&leftLayout);
     mainLayout.addLayout(&rightLayout);
     setLayout(&mainLayout);
    

    And I get
    *** Error in `./findDialog': double free or corruption (out): 0x00007ffde0c74010 ***
    Aborted

    Why is this, another question is this class dynamically allocates all of its widgets and does not have a destructor that frees them. In the book it does specify that the OS will claim it back but isn't that bad practice.

    If Qt is freeing my data for me I don't think I like it because I don't know if I should free something or not. Maybe I'll get used to it as I progress. Cheers!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 27 Oct 2015, 21:05 last edited by
      #2

      Hi,

      You have everything explained here

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

      C 1 Reply Last reply 27 Oct 2015, 21:17
      2
      • S SGaist
        27 Oct 2015, 21:05

        Hi,

        You have everything explained here

        C Offline
        C Offline
        ChajusSaib
        wrote on 27 Oct 2015, 21:17 last edited by
        #3

        @SGaist
        Thanks for the quick reply, I still do not fully understand what happens in the constructor, what is happening when they are stored on the stack instead of the heap?

        As for the second question how I do not tell them that the class(FindDialog) is their parent. Like say label is just a a QLabel initialized as follows:
        label(new QLabel(tr("Find &what:")))

        How does it know FindDialog is its parent?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 27 Oct 2015, 21:36 last edited by SGaist
          #4

          That's basic C++: topLeftLayout, leftLayout, rightLayout and mainLayout all goes out of scope at then end of the constructor so they get destroyed.

          Now on to the Qt part: topLeftLayout is a child of leftLayout and leftLayout and rightLayout have become children of mainLayout so the destruction of mainLayout will also trigger the destruction of leftLayout and rightLayout hence your double free problem.

          You put label in topLeftLayout that you add to leftLayout that you add to mainLayout that you set on your widget.

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

          C 1 Reply Last reply 27 Oct 2015, 22:22
          2
          • S SGaist
            27 Oct 2015, 21:36

            That's basic C++: topLeftLayout, leftLayout, rightLayout and mainLayout all goes out of scope at then end of the constructor so they get destroyed.

            Now on to the Qt part: topLeftLayout is a child of leftLayout and leftLayout and rightLayout have become children of mainLayout so the destruction of mainLayout will also trigger the destruction of leftLayout and rightLayout hence your double free problem.

            You put label in topLeftLayout that you add to leftLayout that you add to mainLayout that you set on your widget.

            C Offline
            C Offline
            ChajusSaib
            wrote on 27 Oct 2015, 22:22 last edited by ChajusSaib
            #5

            @SGaist Thank you mate, that clears it up a lot!

            EDIT: Is there a way for me to know when I should free something? Or will it just come by practice?

            1 Reply Last reply
            0

            5/5

            27 Oct 2015, 22:22

            • Login

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