Memory leak...



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


  • Hi and welcome to devnet,

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



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



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



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.