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

SIGSEGV error when declaring QNetworkReply



  • 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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

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



  • @SGaist

    Yes, I updated my post. Thanks.


Log in to reply