·[solved]Can anyone tell me why it returns me "Segmentation fault"?



  • I just want to get json data on the internet.And i use QNetworkAccessManager ,Qnetworkrequest,Qnetworkreply.So simple function i am going to define! But when i run it , it always returns me that! Damn C++ , i hate it!
    here is my short and simple .h and .cpp file:
    SearchByJson.h:
    #include <QObject>
    #include <QNetworkAccessManager>
    #include <QNetworkReply>

    class SearchByJson : public QObject
    {
    Q_OBJECT
    public:
    SearchByJson() {}
    virtual ~SearchByJson() {}

    Q_INVOKABLE QString startsearch();
    

    private slots:
    void getFinish();

    private:
    QNetworkAccessManager qnam;
    QNetworkReply *reply;
    };

    SearchByJson.cpp:
    #include "searchbyjson.h"
    #include <QUrl>
    #include <QNetworkRequest>
    #include <QDebug>

    QString SearchByJson::startsearch(){
    reply = qnam.get(QNetworkRequest(QUrl("http://so.ard.iyyin.com/s/song_with_out?q=Apologize&page=1&size=3")));
    connect(reply,SIGNAL(finished()),this,SLOT(getFinish()));
    }

    void SearchByJson::getFinish(){
    if (reply->error() == QNetworkReply::NoError)
    {
    qDebug() << "error";
    } else {
    qDebug() << "Got it!";
    }
    }



  • This should not even compile since QString SearchByJson::startsearch() does not return anything.
    First thing to do when you get access violation is to find where in the code you get it.

    for example if you have SearchByJson::getFinish() called when reply is NULL or does not point to valid object this may happen.

    Rule of thumb in C++ - every pointer must be initialized.

    SearchByJson() { reply=NULL; } // will make sure pointer is not going to point to invalid objects

    Then check for nulls:

    void SearchByJson::getFinish(){
    Q_CHECK_PTR( reply ); // will abort and let you know where it did
    if (reply->error() == QNetworkReply::NoError)
    {
    qDebug() << "error";
    } else {
    qDebug() << "Got it!";
    }
    }
    

    on top the way you wrote startsearch you do now want it to be called
    when reply is not not NULL, right?

    QString SearchByJson::startsearch(){
    
    Q_ASSERT( reply == NULL ); // this will assert 
    reply = qnam.get(QNetworkRequest(QUrl("http://so.ard.iyyin.com/s/song_with_out?q=Apologize&page=1&size=3")));
    connect(reply,SIGNAL(finished()),this,SLOT(getFinish()));
    }
    

    finally you probably would want to delete reply when it was finished:

    void SearchByJson::getFinish(){
    Q_CHECK_PTR( reply );
    if (reply->error() == QNetworkReply::NoError)
    {
    qDebug() << "error";
    } else {
    qDebug() << "Got it!";
    }
    delete reply;
    reply = NULL;
    }
    

    Edit: Added code markers - p3c0



  • @alex_malyu YES! According to your guidance, I become calm and find the fault. And i feel better about C++. It's not so bad... It's very nice of you. Thank you !



  • @DidaHarp You can up-vote alex's post if you found it helpful. Also its really good practise to edit your post title and put [solved] at the beginning then it is marked at the top level as solved to everyone : )

    like this:
    [solved] Can anyone tell me why it returns me "Segmentation fault"?



  • @code_fodder Alright!


Log in to reply
 

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