Why does it show me the message, Potential leak of memory when opening a QDialog
Solved
General and Desktop
-
Hi guys, I have the following code:
I make a connection to a QLabel to open a QDialog, to show the app license, through a lambda function, the problem I have is that it shows me the message of, Potential leak of memory, as you can see in the image, any suggestion why this happens.
connect(ui->lblLicencia,&QLabel::linkActivated,this,[&](){ QDialog licenciDlg(this); QVBoxLayout *mainLayOut=new QVBoxLayout(&licenciDlg); QTextEdit *teLicencia=new QTextEdit(&licenciDlg); QFile fileName(":/licencia/licencia.txt"); if(!fileName.open(QFile::ReadOnly | QFile::Text)){ QMessageBox::warning(nullptr,qApp->applicationName(),"Error opening file..\n"+ fileName.errorString()); return; } QString text=fileName.readAll(); teLicencia->setPlainText(text); teLicencia->setReadOnly(true); mainLayOut->addWidget(teLicencia); licenciDlg.setLayout(mainLayOut); licenciDlg.setFixedSize(550,477); licenciDlg.exec(); });
-
@lincoln said in Why does it show me the message, Potential leak of memory when opening a QDialog:
mainLayOut
because you do not delete dynamically allocated memory in case you enter the if(...) part.
Warning explains exactly that.
Same applies to teLicencia... -
@lincoln Actually you could simply move these two lines
QVBoxLayout *mainLayOut=new QVBoxLayout(&licenciDlg); QTextEdit *teLicencia=new QTextEdit(&licenciDlg);
after
if(!fileName.open(QFile::ReadOnly | QFile::Text)){...}
then there is no need to delete anything. Create things when they are needed, not before.