Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Is this object destruction failure?
Forum Updated to NodeBB v4.3 + New Features

Is this object destruction failure?

Scheduled Pinned Locked Moved Solved Qt 6
layoutwidgetparent & childdestructor
4 Posts 3 Posters 91 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.
  • N Offline
    N Offline
    Nima_MJ
    wrote last edited by
    #1

    Why does the following program crash when trying to exit the program?

    #include <QApplication>
    #include <QLayout>
    #include <QLabel>
    
    int main(int argc, char *argv[]) {
        QApplication app(argc, argv);
        QWidget appWidget;
    
        QLabel lblA("A");
        QGridLayout layG;
        layG.addWidget(&lblA, 0, 0);
    
        QLabel lblB("B");
        QVBoxLayout totalLayout;
        totalLayout.addLayout(&layG);
        totalLayout.addWidget(&lblB);
    
        appWidget.setLayout(&totalLayout);
        appWidget.show();
    
        return app.exec();
    }
    

    but if you dynamically allocate memory for just one of layG or totalLayout layouts, it won't crash.
    what is happening here?

    1 Reply Last reply
    0
    • I Offline
      I Offline
      IgKh
      wrote last edited by
      #2

      QObjects destroy their children when they are destroyed, and a layout is automatically the parent of any sub-layout added to it.

      Stack objects are destroyed in opposite order to their creation, so totalLayout gets destroyed first, and it also destroys layG since it is its child object in the QObject hierarchy. The next in order is lblB, but after it layG gets destroyed, but it was already deleted so it is a use after free violation and a crash.

      Dynamically allocating (and leaking) either layout prevents automatic destruction of it so avoids the scenario.

      1 Reply Last reply
      3
      • N Nima_MJ has marked this topic as solved
      • N Offline
        N Offline
        Nima_MJ
        wrote last edited by
        #3

        I get it. but actually QLayout is not responsible for destroying any QWidget that is added to it. QLayouts only destroy QLayouts that are added to them. QWidgets cannot be a "child" of QLayouts. here, totalLayout is responsible for removing layG object, which is its child. So here what actually causes the crash is as follows when stack unwinds at the end:
        First, totalLayout's destructor is called.
        Second, in the destructor, totalLayout evokes "delete" operator on layG (cause totalLayout is responsible for removing layG as its child)
        and here is the problem. layG is stack-allocated and calling "delete" operator on a stack-allocated object is not valid.

        So, if we allocate even one of them on the heap, in both scenarios it obviously would resolve this problem, one by memory leaking and the other by properly deallocating object.

        This is true for the widgets too. the only thing different, is that setLayout() function in widgets makes the widget responsible for the life-time of the QLayouts.

        Changing the order of Layou creations, in this case matters. If I were to create totalLayout before layG, the crash wouldn't have happened. cause layG would've been properly destroyed before totalLayout tried to evoke "delete" operator on it. Though, I'm not sure how it's handled in Qt.

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

          Don't create widgets and especially layouts on the stack at all to avoid such problems.

          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
          2

          • Login

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