Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QMenu using a lot of memory



  • 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!


  • Lifetime Qt Champion

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


  • Lifetime Qt Champion

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



  • @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.


  • Lifetime Qt Champion

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



  • @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));