使用 QNetworkManager 时内存访问错误
Memory access error when using QNetworkManager
我对 C++(尽管我对 C 有一些经验)和 QT 还很陌生。我正在尝试制作一个程序,在用户单击按钮时发布到网站,但每当我尝试访问 QNetworkManager 时,我都会遇到内存访问错误。
我的请求对象的代码如下(略微修剪以显示重要位):
#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;
}
我正在通过调用创建 unifiedRequests 对象:
unifiedRequests requestObj;
在不同的文件中。它在我尝试关闭 SSL 的那一行崩溃了(我们使用的是自签名证书,所以我需要这样做才能发出请求)。有什么想法吗?
谢谢!
您通过调用 "unifiedRequests requestObj;" 创建了 unifiedRequests 对象,当变量 "requestObj" 超出范围时,该对象将被删除。
所以,当接收到信号时,对象已经被销毁了。
尝试通过调用 "unifiedRequests* requestObj = new unifiedRequests();" 来创建您的 unifiedRequests 对象。
当然需要在某处调用"delete requestObj;"来销毁这个对象。何时何地取决于您的应用程序(当您不再需要此对象时)。
要了解差异,请看这里:http://www.tutorialspoint.com/cplusplus/cpp_dynamic_memory.htm
或 google "C++ heap / stack / dynamic allocation"
我对 C++(尽管我对 C 有一些经验)和 QT 还很陌生。我正在尝试制作一个程序,在用户单击按钮时发布到网站,但每当我尝试访问 QNetworkManager 时,我都会遇到内存访问错误。
我的请求对象的代码如下(略微修剪以显示重要位):
#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;
}
我正在通过调用创建 unifiedRequests 对象:
unifiedRequests requestObj;
在不同的文件中。它在我尝试关闭 SSL 的那一行崩溃了(我们使用的是自签名证书,所以我需要这样做才能发出请求)。有什么想法吗?
谢谢!
您通过调用 "unifiedRequests requestObj;" 创建了 unifiedRequests 对象,当变量 "requestObj" 超出范围时,该对象将被删除。
所以,当接收到信号时,对象已经被销毁了。
尝试通过调用 "unifiedRequests* requestObj = new unifiedRequests();" 来创建您的 unifiedRequests 对象。
当然需要在某处调用"delete requestObj;"来销毁这个对象。何时何地取决于您的应用程序(当您不再需要此对象时)。
要了解差异,请看这里:http://www.tutorialspoint.com/cplusplus/cpp_dynamic_memory.htm
或 google "C++ heap / stack / dynamic allocation"