Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED]Connect QNetworkAccessManager::finished signal causes Segmentation Fault



  • hello ,
    i write a code for sending a request a post request to my server and get response in json ,but when i debug it,i see when i want to use QNetworkReply* from my server response it ran to a segmentation fault.
    here is my code :

    void UserView::fillUsers() const
    {
        QNetworkAccessManager *manager = new QNetworkAccessManager(nullptr);
        QUrl Url("http://127.0.0.1:8000/api/getuserchats");
        QNetworkRequest request(Url);
    
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
        QUrlQuery query;
        query.addQueryItem("token","d32e7ad39c6977bce30b8209791af008533e51ca");
    //    query.addQueryItem("password",);
    //    query.addQueryItem("email_address",);
        QNetworkReply* b ;
        QNetworkReply* i = manager->post(request,query.query().toUtf8());
        qDebug()<<query.query().toUtf8();
        connect(manager,&QNetworkAccessManager::finished,this,[&b,&i,&manager,this](){
    //        q_check_ptr(i);
            fff(i);
            Q_UNUSED(this)
            delete manager;});
    }
    
    

    i register this class in qml and when application runs,i instantiat an object of my class in qml code.
    Thank you,sorry for my bad English


  • Moderators

    @ahmad98 said in Connect QNetworkAccessManager::finished signal causes Segmentation Fault:

    QNetworkReply* b ;
    

    This pointer is never set to any value.

    If your request encounters an error, your manager will never get deleted (memory leak).

    delete manager

    At the very least, use deleteLater(). Such use of QNetworkAccessManager is very unconventional. The manager object usually stays active as long as the application is.



  • @sierdzio
    thank you for your reply,so helpful
    also i changed this line of my code and application doesnt crash anymore :
    ```
    connect(manager,&QNetworkAccessManager::finished,i,i,&manager,this{...}


Log in to reply