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. ·[solved]Can anyone tell me why it returns me "Segmentation fault"?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 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.
  • D Offline
    D Offline
    DidaHarp
    wrote on last edited by DidaHarp
    #1

    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!";
    }
    }

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

      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

      D 1 Reply Last reply
      1
      • A alex_malyu

        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

        D Offline
        D Offline
        DidaHarp
        wrote on last edited by
        #3

        @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 !

        C 1 Reply Last reply
        0
        • D DidaHarp

          @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 !

          C Offline
          C Offline
          code_fodder
          wrote on last edited by
          #4

          @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"?

          D 1 Reply Last reply
          0
          • C code_fodder

            @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"?

            D Offline
            D Offline
            DidaHarp
            wrote on last edited by
            #5

            @code_fodder Alright!

            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