Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-11070

QMenu with submenu potential memory leak

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 4.5.2, 4.6.1
    • None
    • Windows XP 64bit, Visual Studio 2005

    Description

      When clearing a QMenu, submenus are only removed, not deleted.

      Example code:
      --------------------------------------------------------------------------------------
      QMenu* menu = new QMenu(NULL);
      QAction* action = menu->addAction("action");
      QMenu* submenu = menu->addMenu("submenu");
      QAction* subaction = submenu->addAction("subaction");

      QPointer<QAction> watchdog1 = action;
      QPointer<QMenu> watchdog2 = submenu;
      QPointer<QAction> watchdog3 = subaction;

      menu->clear(); // "submenu" is only removed not deleted, whereas "action" is deleted
      if (watchdog1==NULL)

      { qDebug("action==NULL (1)"); }

      if (watchdog2==NULL)

      { qDebug("submenu==NULL (1)"); }

      if (watchdog3==NULL)

      { qDebug("subaction==NULL (1)"); }

      delete menu; // "submenu" and "subaction" are deleted now, but this is bad behaviour if we only want to clear and rebuild a menu instead of deleting and reinstantiating it completely
      if (watchdog1==NULL)

      { qDebug("action==NULL (2)"); }

      if (watchdog2==NULL)

      { qDebug("submenu==NULL (2)"); }

      if (watchdog3==NULL)

      { qDebug("subaction==NULL (2)"); }

      --------------------------------------------------------------------------------------

      I don't know if this is the intended behaviour, but since I added the submenu with addMenu(QString) I expected the submenu to be deleted on clear() like the added "action".
      In my case I have a long-living menu which I need to clear and re-fill according to the context, so this issue currently causes a memory leak for me.

      Is it right, that the only chance to overcome this would be to manually delete the submenues on clear somehow or is there something I didn't see?

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            bjnilsen Bjørn Erik Nilsen
            mhoffmann Mark Hoffmann
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes