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. Does QMenuBar::clear() release memory of the action created with ?
Forum Updated to NodeBB v4.3 + New Features

Does QMenuBar::clear() release memory of the action created with ?

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 4.0k Views 3 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.
  • S SGaist
    18 May 2018, 20:10

    Hi,

    Not knowing what else your application is doing can't ensure it's the menu bar that's the culprit.

    In any case, it's a bit strange to completely clear the menu bar so often especially if it's only to update one submenu. You should rather only manipulate what's really needed.

    T Offline
    T Offline
    thelfer
    wrote on 18 May 2018, 20:25 last edited by
    #3

    @SGaist Hi,

    You are perfecly right on the fact that I shall only remove what really needs to be updated.

    However, the main point is that there is a memory leak to I need to solve.

    If I remove the update of the QMenuBar, no more memory leak. If I remove the call to the setIcon method, the memory leak is much lower, so I do suspect that the memory leak is related to the fact that the clear method does not do what I believed.

    BTW, I use Qt 5.7.1 under debian jessie.

    Thanks for you answer.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 18 May 2018, 20:28 last edited by
      #4

      Can you check whether bm is still valid after the call to clear and before overwriting its content ?

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

      T 1 Reply Last reply 18 May 2018, 20:30
      0
      • S SGaist
        18 May 2018, 20:28

        Can you check whether bm is still valid after the call to clear and before overwriting its content ?

        T Offline
        T Offline
        thelfer
        wrote on 18 May 2018, 20:30 last edited by
        #5

        @SGaist How would I ? this->bm is a pointer, there is no way that the clear method would change it, so could I check if it's still valid ?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 18 May 2018, 20:37 last edited by
          #6

          Use a QPointer

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

          T 1 Reply Last reply 18 May 2018, 20:39
          1
          • S SGaist
            18 May 2018, 20:37

            Use a QPointer

            T Offline
            T Offline
            thelfer
            wrote on 18 May 2018, 20:39 last edited by
            #7

            @SGaist Thanks for the tip ! I totally forgot about QPointer. I'll do this tomorrow.

            T 1 Reply Last reply 19 May 2018, 06:03
            0
            • T thelfer
              18 May 2018, 20:39

              @SGaist Thanks for the tip ! I totally forgot about QPointer. I'll do this tomorrow.

              T Offline
              T Offline
              thelfer
              wrote on 19 May 2018, 06:03 last edited by
              #8

              Well, using QPointer shows that this->bm is not released by QMenuBar::clear(). Shall we consider

              • that this is normal and one shall release each items (QMenu and QActions) by hand (though I did not think about how to do it in pratice)
              • this is a bug in QMenuBar
                What do you think ?
                Regards
              1 Reply Last reply
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 19 May 2018, 06:38 last edited by
                #9

                QMenu *QMenuBar::addMenu(const QString &title)

                Appends a new QMenu with title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.

                So when destroying the menu, the submenus will be gone. There is no constraint that those menus are destroyed on clear() esp. since the the documenation does not state anything releated to this:

                Removes all the actions from the menu bar.

                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
                0
                • T Offline
                  T Offline
                  thelfer
                  wrote on 19 May 2018, 07:02 last edited by
                  #10

                  @Christian-Ehrlicher Since the QMenuBar takes the ownership of the sub-menus, I do not feel that deleting them by hand is legal.

                  So, if clear is not meant to release the memory, how to do it ?

                  Even the documentation of the removeAction method does not state that the action is deleted. And there is no removeMenu method that would work on submenus.

                  Any advice is welcomed.

                  M 1 Reply Last reply 19 May 2018, 08:38
                  0
                  • T thelfer
                    19 May 2018, 07:02

                    @Christian-Ehrlicher Since the QMenuBar takes the ownership of the sub-menus, I do not feel that deleting them by hand is legal.

                    So, if clear is not meant to release the memory, how to do it ?

                    Even the documentation of the removeAction method does not state that the action is deleted. And there is no removeMenu method that would work on submenus.

                    Any advice is welcomed.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 19 May 2018, 08:38 last edited by mrjj
                    #11

                    @thelfer
                    Hi
                    Since Actions can be shared between toolbar and menubar, its reasonable that clear() does not
                    free them.
                    Its a bit unusual use case but you could do like.

                    
                    void enumerateMenu(QMenu* menu) {
                      foreach (QAction* action, menu->actions()) {
                        if (action->isSeparator()) {
                          qDebug("this action is a separator");
                        } else if (action->menu()) {
                          qDebug("action: %s", qUtf8Printable(action->text()));
                          qDebug(">>> this action is associated with a submenu, iterating it recursively...");
                          enumerateMenu(action->menu());
                          delete action->menu(); // KILL IT
                          qDebug("<<< finished iterating the submenu");
                        } else {
                          qDebug("action: %s", qUtf8Printable(action->text()));
                          delete action; // KILL IT
                        }
                      }
                    }
                    
                    void MainWindow::on_btRemove_released() {
                      enumerateMenu(bm);
                      delete bm; // bm is member
                    }
                    

                    Seems to clean up as demostrated in this sample where i subclass Aaction to write to qDebug in dtor.
                    https://www.dropbox.com/s/jwlpvpva2mx6cy9/FreeMe.zip?dl=0

                    I also instrumented QIcon but is assigned by copy so that was not informative.

                    enumerateMenu code credits to
                    https://stackoverflow.com/questions/9399840/how-to-iterate-through-a-menus-actions-in-qt

                    1 Reply Last reply
                    2
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 19 May 2018, 09:01 last edited by
                      #12

                      @thelfer said in Does QMenuBar::clear() release memory of the action created with ?:

                      So, if clear is not meant to release the memory, how to do it ?

                      It's clearly documented:

                      The menu bar takes ownership of the menu.

                      --> When the QMenuBar is destroyed, the QActions will be deleted too.

                      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
                      • M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 19 May 2018, 09:12 last edited by
                        #13

                        Hi
                        Fooling around with it, maybe just total nuke works best

                        void MainWindow::on_btDelete_released() {
                        delete ui->menuBar;
                        this->setMenuBar( new QMenuBar(this) );
                        }

                        it dont even flashes and clearly delete all.

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          thelfer
                          wrote on 19 May 2018, 16:39 last edited by
                          #14

                          Well, I tested @mrjj approach, and it led to a segfault right from the start. I'll investigate.

                          M 1 Reply Last reply 20 May 2018, 06:47
                          0
                          • T thelfer
                            19 May 2018, 16:39

                            Well, I tested @mrjj approach, and it led to a segfault right from the start. I'll investigate.

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 20 May 2018, 06:47 last edited by mrjj
                            #15

                            @thelfer
                            Oh.
                            enumerateMenu crashed ?
                            I tested it with several subitems.

                            Do you have multiple "items" ?

                            alt text

                            Did sample crash for you? or was in first when used in real code?

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              thelfer
                              wrote on 20 May 2018, 08:41 last edited by
                              #16

                              Well, I found the reason of the segfault: I hold one action as a member. This action was deleted by enumateMenu and the code crashes when this action was added again, which is perfectly normal. I think that I have to refactor my code.
                              Thanks to everyone.

                              M 1 Reply Last reply 20 May 2018, 08:44
                              1
                              • T thelfer
                                20 May 2018, 08:41

                                Well, I found the reason of the segfault: I hold one action as a member. This action was deleted by enumateMenu and the code crashes when this action was added again, which is perfectly normal. I think that I have to refactor my code.
                                Thanks to everyone.

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 20 May 2018, 08:44 last edited by
                                #17

                                @thelfer
                                Ok. good found.
                                I assume its a no go to wipe whole menubar and recreate ?
                                You are keeping some when u "refresh" ?
                                delete ui->menuBar;
                                this->setMenuBar( new QMenuBar(this) );

                                1 Reply Last reply
                                0

                                12/17

                                19 May 2018, 09:01

                                • Login

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