Important: Please read the 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("");
        QNetworkRequest request(Url);
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
        QUrlQuery query;
    //    query.addQueryItem("password",);
    //    query.addQueryItem("email_address",);
        QNetworkReply* b ;
        QNetworkReply* i = manager->post(request,query.query().toUtf8());
    //        q_check_ptr(i);
            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 :

Log in to reply