gpt4 book ai didi

c++ - 使用 QNetworkManager 时内存访问错误

转载 作者:行者123 更新时间:2023-11-28 02:04:43 24 4
gpt4 key购买 nike

我对 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;”创建统一请求对象,当变量“requestObj”超出范围时,该对象将被删除。

因此,当接收到信号时,对象将已经被销毁。

尝试通过调用“unifiedRequests* requestObj = new unifiedRequests();”来创建您的 unifiedRequests 对象。

当然需要调用“delete requestObj;”在某处销毁此对象。何时何地取决于您的应用程序(当您不再需要此对象时)。

要了解差异,请看这里:http://www.tutorialspoint.com/cplusplus/cpp_dynamic_memory.htm

或谷歌搜索“C++ 堆/栈/动态分配”

关于c++ - 使用 QNetworkManager 时内存访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37984829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com