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. Memory leak...
Forum Updated to NodeBB v4.3 + New Features

Memory leak...

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 2.1k Views 2 Watching
  • 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.
  • E Offline
    E Offline
    Ericode
    wrote on last edited by Ericode
    #1

    Hello, each time this button is pressed there seems to be a memory leak and I am not sure where it is coming from. Can anyone spot it?

    void MainWindow::on_LoginPushButton_clicked()
    {
        QString username, password;
        QSqlQuery qry;
    
        username = ui->lineEdit_Username->text();
        password = ui->lineEdit_Password->text();
    
    
        QUrl url = ("url entered here");
        QNetworkRequest request( url );
        request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/x-www-form-urlencoded"));
    
        QJsonObject loginInfo;
        loginInfo["username"] = username;
        loginInfo["password"] = password;
    
        QJsonDocument doc(loginInfo);
    
        QByteArray bytes = doc.toJson();
    
        QNetworkAccessManager *nam = new QNetworkAccessManager();
    
        QNetworkReply *reply = nam->post(request,bytes);
    
        connect(reply, &QNetworkReply::finished, [=] {
            strReply = (QString)reply->readAll();
            QJsonDocument jsonDoc = QJsonDocument::fromJson(strReply.toUtf8());
            QJsonObject jsonObject = jsonDoc.object();
    
            if ( jsonObject["status"].operator ==("OK")) {
                ui->Username_Status->setText("<font color = 'green'>Successful Connection!</font>");
            }
            else {
                ui->Username_Status->setText("<font color = 'red'>Incorrect Username or Password</font>");
            }
            delete reply;
            delete nam;
        });
    }
    
    O 1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      have you tried to use some tool to check memory usage (for instance http://valgrind.org/)??

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      E 1 Reply Last reply
      0
      • M mcosta

        Hi and welcome to devnet,

        have you tried to use some tool to check memory usage (for instance http://valgrind.org/)??

        E Offline
        E Offline
        Ericode
        wrote on last edited by
        #3

        @mcosta
        Hello, Yes I tried that and it was telling me delete nam; is the memory leak but I am confused about that.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alex_malyu
          wrote on last edited by
          #4

          First check if connect actually connected anything.
          bool c = connect.....

          1 Reply Last reply
          0
          • E Ericode

            Hello, each time this button is pressed there seems to be a memory leak and I am not sure where it is coming from. Can anyone spot it?

            void MainWindow::on_LoginPushButton_clicked()
            {
                QString username, password;
                QSqlQuery qry;
            
                username = ui->lineEdit_Username->text();
                password = ui->lineEdit_Password->text();
            
            
                QUrl url = ("url entered here");
                QNetworkRequest request( url );
                request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/x-www-form-urlencoded"));
            
                QJsonObject loginInfo;
                loginInfo["username"] = username;
                loginInfo["password"] = password;
            
                QJsonDocument doc(loginInfo);
            
                QByteArray bytes = doc.toJson();
            
                QNetworkAccessManager *nam = new QNetworkAccessManager();
            
                QNetworkReply *reply = nam->post(request,bytes);
            
                connect(reply, &QNetworkReply::finished, [=] {
                    strReply = (QString)reply->readAll();
                    QJsonDocument jsonDoc = QJsonDocument::fromJson(strReply.toUtf8());
                    QJsonObject jsonObject = jsonDoc.object();
            
                    if ( jsonObject["status"].operator ==("OK")) {
                        ui->Username_Status->setText("<font color = 'green'>Successful Connection!</font>");
                    }
                    else {
                        ui->Username_Status->setText("<font color = 'red'>Incorrect Username or Password</font>");
                    }
                    delete reply;
                    delete nam;
                });
            }
            
            O Offline
            O Offline
            onek24
            wrote on last edited by onek24
            #5

            Hello @Ericode ,

            thats what the QtDocs say about your problem(or the finished signal): Link

            Note: Do not delete the object in the slot connected to this signal. Use deleteLater().

            All you have to do is to replace the delete in your lambda expression with deleteLater():

            nam.deleteLater();
            reply.deleteLater();
            

            So your full code will look like this:

            void MainWindow::on_LoginPushButton_clicked()
            {
                QString username, password;
                QSqlQuery qry;
            
                username = ui->lineEdit_Username->text();
                password = ui->lineEdit_Password->text();
            
            
                QUrl url = ("url entered here");
                QNetworkRequest request( url );
                request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/x-www-form-urlencoded"));
            
                QJsonObject loginInfo;
                loginInfo["username"] = username;
                loginInfo["password"] = password;
            
                QJsonDocument doc(loginInfo);
            
                QByteArray bytes = doc.toJson();
            
                QNetworkAccessManager *nam = new QNetworkAccessManager();
            
                QNetworkReply *reply = nam->post(request,bytes);
            
                connect(reply, &QNetworkReply::finished, [=] {
                    strReply = (QString)reply->readAll();
                    QJsonDocument jsonDoc = QJsonDocument::fromJson(strReply.toUtf8());
                    QJsonObject jsonObject = jsonDoc.object();
            
                    if ( jsonObject["status"].operator ==("OK")) {
                        ui->Username_Status->setText("<font color = 'green'>Successful Connection!</font>");
                    }
                    else {
                        ui->Username_Status->setText("<font color = 'red'>Incorrect Username or Password</font>");
                    }
                    reply.deleteLater();
                    nam.deleteLater();
                });
            }
            

            For further informations about the deleteLater() read: QObject#deleteLater

            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