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. Why does it show me the message, Potential leak of memory when opening a QDialog

Why does it show me the message, Potential leak of memory when opening a QDialog

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 488 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.
  • lincolnL Offline
    lincolnL Offline
    lincoln
    wrote on last edited by
    #1

    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();
    
    
      });
    

    21291c9a-dea2-4e3f-84d2-c96dc6b20968-image.png

    Solitary wolf

    jsulmJ 1 Reply Last reply
    0
    • lincolnL lincoln

      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();
      
      
        });
      

      21291c9a-dea2-4e3f-84d2-c96dc6b20968-image.png

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      lincolnL 1 Reply Last reply
      1
      • jsulmJ jsulm

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

        lincolnL Offline
        lincolnL Offline
        lincoln
        wrote on last edited by lincoln
        #3

        @jsulm
        Ok great, if now the warning is no longer displayed, thanks for your answer.

        add these two lines

        delete mainLayOut;
        delete teLicencia;
        

        Solitary wolf

        jsulmJ 1 Reply Last reply
        0
        • lincolnL lincoln

          @jsulm
          Ok great, if now the warning is no longer displayed, thanks for your answer.

          add these two lines

          delete mainLayOut;
          delete teLicencia;
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          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