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. QMenu using a lot of memory
Forum Update on Monday, May 27th 2025

QMenu using a lot of memory

Scheduled Pinned Locked Moved Solved General and Desktop
qmencontex menumemorymemory leak
5 Posts 3 Posters 1.0k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello everyone,

    for a simple Qt application I created a custom context menu, which pops up, when the user issues a right click. However, when the QMenu is created, the memory usage of the program increases by ~10MiB, according to the system manager. The memory is not released when the menu disappears and so the memory usage skyrockets when the user creates multiple context menus.

    This is the code for creating and displaying the menu:

    // In the MainWindow constructor
    connect(ui->textArea, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenu(QPoint)));
    
    void MainWindow::contextMenu(const QPoint &pos) {
        QMenu* menu = new QMenu;
        QAction* newNote = menu->addAction(QIcon(":/icons/newNote.ico"), tr("New"), this, SLOT(newNote()));
        newNote->setShortcut(Qt::CTRL | Qt::Key_N);
        QAction* open = menu->addAction(QIcon(":/icons/open.ico"), tr("Open"), this, SLOT(open()));
        open->setShortcut(Qt::CTRL | Qt::Key_O);
        QAction* save = menu->addAction(QIcon(":/icons/save.ico"), tr("Save"), this, [this]{saveNote(currentFile);});
        save->setShortcut(Qt::CTRL | Qt::Key_S);
        QAction* saveAs = menu->addAction(QIcon(":/icons/saveAs.ico"), tr("Save As"), this, [this]{saveNote("");});
        saveAs->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_S);
    
        QMenu* settings = new QMenu("Settings", this);
        settings->setIcon(QIcon(":/icons/settings.ico"));
        settings->addAction(QIcon(":/icons/toggle.ico"), tr("Toggle Frameless"), this, SLOT(toggleFrameless()));
        settings->addAction(QIcon(":/icons/textColor.ico"), tr("Choose Text Color"), this, SLOT(chooseTextColor()));
        settings->addAction(QIcon(":/icons/backgroundColor.ico"), tr("Choose Background Color"), this, SLOT(chooseBackgroundColor()));
        settings->addAction(QIcon(":/icons/font.ico"), tr("Choose Font"), this, SLOT(chooseFont()));
        menu->addMenu(settings);
    
        QAction* close = menu->addAction(QIcon(":/icons/close.ico"), tr("Close Window"), this, SLOT(closeWindow()));
        close->setShortcut(Qt::CTRL | Qt::Key_Q);
    
        menu->exec(mapToGlobal(pos));
    }
    

    The icons I use sum up to about 140kB.
    I am working on Ubuntu 20.10 with Qt 6.0.2.

    Thank you very much in advance!

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Why do you wonder that the memory is not released when you don't delete the menu pointer afterwards after usage??

      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
      1
      • Christian EhrlicherC Christian Ehrlicher

        Why do you wonder that the memory is not released when you don't delete the menu pointer afterwards after usage??

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @Christian-Ehrlicher Do you mean that I have to add some kind of delete menu at the end of the function? I am new to Qt and C++ in general, so please excuse if this is a trivial question.

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

          C++ basics - when you create an object on the heap you also have to destroy it when you don't need it later on.

          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
          • nageshN Offline
            nageshN Offline
            nagesh
            wrote on last edited by
            #5

            @Roysten-Rigobert Why don't you create menu/actions at once by making menu as member variable?

            and in contextMenu(QPoint) slot call only the menu->exec(mapToGlobal(pos));

            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