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. Removing layoutitem and deleting it yet still memory leak
Qt 6.11 is out! See what's new in the release blog

Removing layoutitem and deleting it yet still memory leak

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.8k 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.
  • T Offline
    T Offline
    theuser
    wrote on last edited by
    #1

    Hi,

    for fun i created some sort of dice rolling app.
    It allows you to roll any amount of dices with any amount of pips. The result is shown as simply QLabels with the dices pips on them.
    I do this by having a seperate layout for the "result labels" that is added to my central layout and adding the needed amount of QLabels to it with every roll.
    At the start of the drawing of the result i simply erase the content of the "result layout".
    This apparently creates a memory leak.
    Here is the important part of my code:

    @void Widget::onRoll(void) //this is a slot
    {
    quint32 dicenumber = diceinput.text().toUInt();//how many dices were used
    quint32 dicetype = this->dicetype.text().toUInt(); //amount of pips
    clearLayout(diceresult);

    for(quint32 i = 0; i < dicenumber;i++)
    {
        diceresult.addWidget(QPointer<QLabel>(new QLabel(QString::number((qrand() % dicetype) + 1))));
    }
    
    update();
    

    }
    //this apparently results in a leak
    void Widget::clearLayout(QLayout& layout)
    {
    QLayoutItem* item;
    while(item = layout.takeAt(0))
    {
    item->widget()->setVisible(false);
    layout.removeWidget(item->widget());
    delete item;
    }
    }
    @

    When i click the "roll dice" button of my program it calls the "onRoll" slot.
    Why does the "delete item" call not lead to all dynamic memory being properly deleted?

    Btw i figured this had a leak by looking at the rising amount of memory the program used (provided by windows task manager)

    Any help would be great :)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You also need to delete the widget

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

      1 Reply Last reply
      0
      • T Offline
        T Offline
        theuser
        wrote on last edited by
        #3

        Hi,

        thank you :) This solved it.
        But i would like to understand why this worked and my first approach didnt.

        what i do now is simply
        @ QLayoutItem* item;
        while((item = layout.takeAt(0)) != 0)
        {
        item->widget()->setVisible(false);
        delete item->widget();
        delete item;
        }@

        why is the dynamic memory not released by calling "delete item"?

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          because the item doesn't take ownership of the widget itself... IIRC this should be mentioned somewhere in the docs.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          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