Search code examples
c++qtqnetworkaccessmanager

Memory access error when using QNetworkManager


I'm fairly new to C++ (though I have some experience with C) as well as QT. I'm trying to make a program that POSTs to a website when the user clicks a button, but whenever I try to access QNetworkManager I get a memory access error.
The code for my request object is as follows (trimmed slightly to show the important bits):

#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include "cJSON.h"

class unifiedRequests: public QObject {
    Q_OBJECT
public:
// Members
    QString access_token = "";
    bool admin = false;
// Methods
    explicit unifiedRequests(QObject *parent=0);
    QNetworkReply* login_request(QString *email, QString *password);

signals:

public slots:
    void login_complete(QNetworkReply *reply);
    void sslErrorHandler(QNetworkReply*, const QList<QSslError> & );

private:
    QNetworkRequest make_headers(QByteArray endpoint);
    QNetworkRequest make_headers(QByteArray endpoint, QByteArray *access_token);
};

QNetworkRequest unifiedRequests::make_headers(QByteArray endpoint) {
    QString url = endpoint.prepend("https://dali.vpt.co.uk");
    QNetworkRequest request = QNetworkRequest(url);
    qDebug() << "Setting Headers";
    request.setRawHeader("User-Agent", "Desktop Client Debug");
    request.setRawHeader("Content-Type", "application/json");
    qDebug() << "Set headers successfully.";
    return request;
}

void unifiedRequests::sslErrorHandler
(QNetworkReply* reply, const QList<QSslError> & errors) {
    qDebug() << "Ignoring SSL Errors";
};

QNetworkReply* unifiedRequests::login_request
(QString *email, QString *password) {
    QNetworkRequest request = make_headers("/api/auth");

    qDebug() << "Making JSON";
    cJSON *login_json; //The body of the request
    login_json = cJSON_CreateObject();
    cJSON_AddStringToObject(login_json, "email", email->toUtf8());
    cJSON_AddStringToObject(login_json, "password", password->toUtf8());
    qDebug() << "Made JSON: ";
    qDebug() << cJSON_Print(login_json);

    QNetworkAccessManager *manager =  new QNetworkAccessManager;
    //The object we use to send the request and receive the reply
    qDebug() << "Turning off SSL";
    connect(manager,
        SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError> & )),
        this,
        SLOT(sslErrorHandler(QNetworkReply*, const QList<QSslError> &     )));
    qDebug() << "POSTing login.";
    QNetworkReply *reply = manager->post(request, cJSON_Print(login_json));

    qDebug() << "Connecting signal to slot.";
    QAbstractSocket::connect(manager, SIGNAL(finished(QNetworkReply * )),
                         this, SLOT(login_complete(QNetworkReply * )));
    cJSON_Delete(login_json);
    return reply;
}

I'm creating the unifiedRequests object by calling:

unifiedRequests requestObj;

in a different file. It crashes out on the line where I try to turn off SSL (we're using a self-signed certificate, so I need to do this in order to make the request). Any thoughts?
Thank you!


Solution

  • You create the unifiedRequests object by calling "unifiedRequests requestObj;", this object will be deleted when the variable "requestObj" goes out of scope.

    So, when the signal will be received, the object will be already destroyed.

    Try to create your unifiedRequests object by calling "unifiedRequests* requestObj = new unifiedRequests();".

    Of course, you need to call "delete requestObj;" somewhere to destroy this object. Where and when depend on your application (when you don't need this object anymore).

    To understand the difference, look at here : http://www.tutorialspoint.com/cplusplus/cpp_dynamic_memory.htm

    Or google for "C++ heap / stack / dynamic allocation"