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. SIGSEGV error when declaring QNetworkReply
Qt 6.11 is out! See what's new in the release blog

SIGSEGV error when declaring QNetworkReply

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.2k Views 1 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.
  • T Offline
    T Offline
    TokiiWalkie
    wrote on last edited by
    #1

    Hello everyone. I am working on my first Qt project. For this project I need to use the spotify API. I have seen on internet how to make an http request.

    When I debug my program, I have a SIGSEGV error on my QNetworkReply declaration. Here's the error :
    031cf38f-a827-4e27-bec3-969cb4ad2736-image.png

    Here's my .cpp file :

    #include "spotifyplayer.h"
    #include "QtNetwork/QNetworkAccessManager"
    #include "QtNetwork/QNetworkRequest"
    #include "QtNetwork/QNetworkReply"
    #include "QObject"
    
    #include <iostream>
    #include <sstream>
    
    std::string authorization;
    std::string authBearer;
    
    SpotifyPlayer::SpotifyPlayer(std::string auth) {
        authorization = auth;
        authBearer = "Bearer " + authorization;
    }
    
    void SpotifyPlayer::pauseMusic() {
        QByteArray authByteArray(authBearer.c_str(), authBearer.length());
        std::cout << authBearer << std::endl;
    
        QNetworkAccessManager *manager;
        QNetworkRequest request(QUrl("https://api.spotify.com/v1/me/player/pause"));
        request.setRawHeader("Accept", "application/json");
        request.setRawHeader("Content-Type", "application/json");
        request.setRawHeader("Authorization", authByteArray);
    
        QString postVar = "device_id=709017e7c2fbc5e92ce9e80b9f983f25428e6916";
        QNetworkReply *res = SpotifyPlayer::manager->put(request, postVar.toUtf8());
    }
    

    .h :

    #ifndef SPOTIFYPLAYER_H
    #define SPOTIFYPLAYER_H
    
    #include <iostream>
    
    #include <QtNetwork/QNetworkAccessManager>
    
    class SpotifyPlayer : QObject
    {
    Q_OBJECT
    public:
        SpotifyPlayer(std::string authorization);
        void pauseMusic();
    private:
        QNetworkAccessManager *manager;
        QNetworkRequest request;
        QNetworkReply *res;
    
        std::string authorization;
        std::string authBearer;
        QByteArray authByteArray;
    };
    
    #endif // SPOTIFYPLAYER_H
    

    Thank you all ! :D

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      @TokiiWalkie said in SIGSEGV error when declaring QNetworkReply:

      QNetworkAccessManager *manager;

      You do not initialise your manager class member variable nor the function local variable of the same name.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • T Offline
        T Offline
        TokiiWalkie
        wrote on last edited by
        #3

        ok, so what should i write now?
        (sorry, im really new ^^')

        Cobra91151C 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Well, it's basic C++ pointer handling. You need to allocate a QNetworkAccessManager object.

          If this is cryptic, then I encourage you to take some time to learn the ropes of the language before going further.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • T TokiiWalkie

            ok, so what should i write now?
            (sorry, im really new ^^')

            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by Cobra91151
            #5

            @TokiiWalkie

            In your case, you have 2 issues. The first one, you created 2 QNetworkAccessManager objects:

            private:
                QNetworkAccessManager *manager;
            

            And

            void SpotifyPlayer::pauseMusic() {
                QByteArray authByteArray(authBearer.c_str(), authBearer.length());
                std::cout << authBearer << std::endl;
            
                QNetworkAccessManager *manager;
            

            Keep in mind, you can only have 1 QNetworkAccessManager *manager; object for the whole program. The second issue, you do not allocate the created QNetworkAccessManager object.

            So, to fix it you have to replace this line: QNetworkAccessManager *manager; from pauseMusic() method with manager = new QNetworkAccessManager(this);

            Code:

            void SpotifyPlayer::pauseMusic() {
                QByteArray authByteArray(authBearer.c_str(), authBearer.length());
                std::cout << authBearer << std::endl;
            
                manager = new QNetworkAccessManager(this);
                QNetworkReply *res = SpotifyPlayer::manager->put(request, postVar.toUtf8());
                
                // You need to deallocate the object and free the memory to prevent the memory leaks
                // Check out the code below
                connect(res, &QNetworkReply::finished, [this, res]() {
                   res->close();
                   res->deleteLater();
                   manager->deleteLater();   
                }
            

            Or initialize it only in the constructor:

            SpotifyPlayer::SpotifyPlayer(std::string auth) {
                authorization = auth;
                authBearer = "Bearer " + authorization;
                manager = new QNetworkAccessManager(this);
            }
            

            Happy coding!

            1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Cobra91151 said in SIGSEGV error when declaring QNetworkReply:

              void SpotifyPlayer::pauseMusic() {
              QByteArray authByteArray(authBearer.c_str(), authBearer.length());
              std::cout << authBearer << std::endl;

              manager = new QNetworkAccessManager(this);
              

              This one is problematic as it will create a new instance of QNetworkAccessManager each time pauseMusic is called.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              Cobra91151C 1 Reply Last reply
              1
              • SGaistS SGaist

                @Cobra91151 said in SIGSEGV error when declaring QNetworkReply:

                void SpotifyPlayer::pauseMusic() {
                QByteArray authByteArray(authBearer.c_str(), authBearer.length());
                std::cout << authBearer << std::endl;

                manager = new QNetworkAccessManager(this);
                

                This one is problematic as it will create a new instance of QNetworkAccessManager each time pauseMusic is called.

                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by Cobra91151
                #7

                @SGaist

                Yes, I updated my post. Thanks.

                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