Silnik
Silnik

Reputation: 328

Qt: QMenu seems to not be deleted after deleting MainWindow

In the constructor of MainWindow i have a chunk of code:

QMenu * filemenu = this->menuBar()->addMenu(tr("File"));
QAction * openButton = new QAction(tr("Open"), this);
connect(openButton, SIGNAL(triggered()), this, SLOT(input()));
filemenu->addAction(openButton);

Everything seemed to be fine until I ran a memory check. Valgrind seems to be saying that there is a memory leak here. Shouldn’t the QMenu be automatically deleted right before MainWindow is deleted? I tried remembering the pointer to filemenu and deleting it manually in the MainWindow destructor but it didn't change anything. Does anyone have an idea what am I doing wrong?

Upvotes: 1

Views: 466

Answers (1)

László Papp
László Papp

Reputation: 53155

Shouldn’t the QMenu be automatically deleted right before MainWindow is deleted?

You could connect a slot to the destroyed() signal of your QMenu instance (filemenu) and print out something there with qDebug(). If that gets printed for the mainwindow destruction that means the destructor is called, i.e. deleted.

...
   connect(myMenuPointer, SIGNAL(destroyed()), receiverPointer, SLOT(test()));
...

MyClass::test()
{
    qDebug() << "My menu deleted automatically";
}

Sometimes, there is memory leak detected in a Qt application by valgrind if something underneath leaks the memory like glibc and so on. It might be the case, but the answer to your question is that QMenu will be automatically deleted.

Upvotes: 1

Related Questions