Memory leak related to open file dialog
-
The following function introduces a memory leak: Even if you just close the dialog box without opening the file, memory will not be reclaimed. A memory leak occurs when the dialog is opened and closed.
When you first run the program, it takes up 12MB of space. When you open the dialog box, it becomes 19MB and when you close it, it becomes 18MB. A memory leak occurs.void WpubEditor::on_Open_wpub_triggered() { QString fileName = QFileDialog::getOpenFileName(this, tr("Open trv file"), "", tr("TRV Files (*.trv)")); if (!fileName.isEmpty()) { if(m_pPubManager && m_pPubManager->getModify()){ qDebug() << "modify " << m_pPubManager->getModify(); on_Save_triggered(); } clear(); // Release memory if m_pPubManager already points to an object delete m_pPubManager; m_pPubManager = nullptr; m_pPubManager = new WPubManager(*paintBox, nullptr); paintBox->getManager(m_pPubManager); if(m_pPubManager->OpenWPub(fileName)){ updateStatusBar(fileName); //ui->Save_Wpub->setEnabled(false); //ui->Close_File->setEnabled(true); setMenuActionStatus(true); }else{ qWarning("Couldn't open file."); } } }
I modified it as follows. I could see that the dialog was deleted normally. But the memory leak didn't go away. I don't think this is a problem with dialog. Are there other factors that cause memory leaks? How can I solve this?
QString fileName; QFileDialog* dialog = new QFileDialog(this); dialog->setWindowTitle(tr("Open trv file")); dialog->setFileMode(QFileDialog::ExistingFile); dialog->setNameFilter(tr("trv Files (*.trv)")); if (dialog->exec()) { QStringList fileNames = dialog->selectedFiles(); fileName = fileNames.first(); } delete dialog; dialog = nullptr; if (!fileName.isEmpty()) { if(m_pPubManager && m_pPubManager->getModify()){ qDebug() << "modify " << m_pPubManager->getModify(); on_Save_triggered(); } clear(); m_pPubManager = new WPubManager(*paintBox, nullptr); paintBox->getManager(m_pPubManager); if(m_pPubManager->OpenWPub(fileName)){ updateStatusBar(fileName); setMenuActionStatus(true); } else { qWarning("Couldn't open file."); } } if (dialog == nullptr) { qDebug() << "dialog is deleted successfully."; } else { qDebug() << "dialog is not properly deleted!"; }
-
@MyNameIsQt
You did not even say how you measured the increase in memory usage.When code requests memory to allocate the runtime hands it out from pools of available memory it holds. If the pools do not have enough available spare memory more is requested from the OS. When code
free
s (or whatever) memory it is not returned to the OS. It is however assigned back to the available pools. The hope is that, over time, that memory may be reused for other allocations in that program.You cannot accurately measure allocations/frees/leaks by looking at, say, the output from Windows taskman. You need a proper tool which analyzes your program's actual usage, such as valgrind or equivalent in MSVC.
-
@MyNameIsQt said in Memory leak related to open file dialog:
When you first run the program, it takes up 12MB of space. When you open the dialog box, it becomes 19MB and when you close it, it becomes 18MB. A memory leak occurs.
On modern OS it doesn't mean nothing.
If the memory continues to grow in time ( after one hour of use) then yes there's a problem.
You can try some tool like valgrind and see if it finds leaks.since you use if (dialog->exec())
you can use a local variable:QFileDialog dialog;
no need to delete it anymore.
-
Hi mpergand!
So what is unreclaimed memory storage space? Why isn't it being recalled? Is that a bug? For some reason, it doesn't look neat to leave it like that.
Thanks.. -
@MyNameIsQt said in Memory leak related to open file dialog:
So what is unreclaimed memory storage space? Why isn't it being recalled? Is that a bug? For some reason, it doesn't look neat to leave it like that.
As @mpergand said, watch it over a longer period of time. Only because your program memory does not shrink instantly to the state it had before, you don't have necessarily a mem leak in your code.
If you call this function like 20 times and the memory constantly increases, it might be an indicator, but I can't see an obvious mem leak in your code if you clean up the dialog properly. Or do as @mpergand suggested. -
@MyNameIsQt
You did not even say how you measured the increase in memory usage.When code requests memory to allocate the runtime hands it out from pools of available memory it holds. If the pools do not have enough available spare memory more is requested from the OS. When code
free
s (or whatever) memory it is not returned to the OS. It is however assigned back to the available pools. The hope is that, over time, that memory may be reused for other allocations in that program.You cannot accurately measure allocations/frees/leaks by looking at, say, the output from Windows taskman. You need a proper tool which analyzes your program's actual usage, such as valgrind or equivalent in MSVC.
-
@JonB
Thank you. Your comment seems very important to me. Because I simply checked in 'window taskman'.
In fact, when working with visual studio, I was able to get accurate information about memory usage. However, in QT, I simply checked it with window taskman. There seems to be a need for a tool that can monitor memory in real time.
Your advice is very important to me. thank you so much. -