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. Memory management in the calendarwidget example. Is there are memory leak?

Memory management in the calendarwidget example. Is there are memory leak?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 356 Views 1 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.
  • D Offline
    D Offline
    dgrigoro
    wrote on last edited by dgrigoro
    #1

    Hello, this is my first time posting here!

    I am new to C++ and Qt and I was looking at the example source code. Specifically, this file:

    https://github.com/qt/qtbase/blob/dev/examples/widgets/widgets/calendarwidget/window.cpp

    and this member function:

    void Window::createPreviewGroupBox()
    {
        previewGroupBox = new QGroupBox(tr("Preview"));
    
        calendar = new QCalendarWidget;
        calendar->setMinimumDate(QDate(1900, 1, 1));
        calendar->setMaximumDate(QDate(3000, 1, 1));
        calendar->setGridVisible(true);
    
        connect(calendar, &QCalendarWidget::currentPageChanged,
                this, &Window::reformatCalendarPage);
    
        previewLayout = new QGridLayout;
        previewLayout->addWidget(calendar, 0, 0, Qt::AlignCenter);
        previewGroupBox->setLayout(previewLayout);
    }
    

    previewGroupBox, calendar and previewLayout are naked pointers to of type QGroupBox* QGridLayout* and QCalendarWidget*. My question is: When and where is the memory allocated using new released? Those objects have no parent to delete them and delete is not called anywhere is the example code.

    Thanks in advance!

    JonBJ 1 Reply Last reply
    0
    • D dgrigoro

      Hello, this is my first time posting here!

      I am new to C++ and Qt and I was looking at the example source code. Specifically, this file:

      https://github.com/qt/qtbase/blob/dev/examples/widgets/widgets/calendarwidget/window.cpp

      and this member function:

      void Window::createPreviewGroupBox()
      {
          previewGroupBox = new QGroupBox(tr("Preview"));
      
          calendar = new QCalendarWidget;
          calendar->setMinimumDate(QDate(1900, 1, 1));
          calendar->setMaximumDate(QDate(3000, 1, 1));
          calendar->setGridVisible(true);
      
          connect(calendar, &QCalendarWidget::currentPageChanged,
                  this, &Window::reformatCalendarPage);
      
          previewLayout = new QGridLayout;
          previewLayout->addWidget(calendar, 0, 0, Qt::AlignCenter);
          previewGroupBox->setLayout(previewLayout);
      }
      

      previewGroupBox, calendar and previewLayout are naked pointers to of type QGroupBox* QGridLayout* and QCalendarWidget*. My question is: When and where is the memory allocated using new released? Those objects have no parent to delete them and delete is not called anywhere is the example code.

      Thanks in advance!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @dgrigoro
      When you addWidget() or setLayout() the objects all go into the widget hierarchy --- previewGroupBox here --- and you'll see parent get set. So those now belong to their parent hierarchy and get disposed with that.

      The previewGroupBox is currently indeed orphaned. To use this method one would either add that into the widget hierarchy here or return it from the function so the caller can do that. In fact looking at the link it's a member variable, so it doesn't get lost, and the caller goes layout->addWidget(previewGroupBox), so that's how it gets connected.

      If you haven't already done so, you'll want to read https://doc.qt.io/qt-5/objecttrees.html & https://doc.qt.io/qt-5/layout.html.

      D 1 Reply Last reply
      2
      • JonBJ JonB

        @dgrigoro
        When you addWidget() or setLayout() the objects all go into the widget hierarchy --- previewGroupBox here --- and you'll see parent get set. So those now belong to their parent hierarchy and get disposed with that.

        The previewGroupBox is currently indeed orphaned. To use this method one would either add that into the widget hierarchy here or return it from the function so the caller can do that. In fact looking at the link it's a member variable, so it doesn't get lost, and the caller goes layout->addWidget(previewGroupBox), so that's how it gets connected.

        If you haven't already done so, you'll want to read https://doc.qt.io/qt-5/objecttrees.html & https://doc.qt.io/qt-5/layout.html.

        D Offline
        D Offline
        dgrigoro
        wrote on last edited by
        #3

        @JonB Thank you! It's all clear now :)

        1 Reply Last reply
        0

        • Login

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