0

我创建了一个库,它将处理所有 HTTP 请求并解析 JSON 格式的响应数据。当我在我的主应用程序(使用 GUI)中调用包含 get 请求的方法时,我收到了内存损坏错误。所以我添加了 QEventLoop 和一个计时器来等待响应,然后再继续其他进程。我可以通过调用 QNetworkReply.readall() 来获取响应数据。我需要获取响应数据的 char* 值,所以我调用了 QNetworkReply.data() 但它是空的。为什么?

以下是我写的代码:

处理 HTTP 请求的库:

void HttpRequest::getRequest(string param1, string param2)
{
    pManager_ = new QNetworkAccessManager(this);

    QUrl cUrl(sampleUrl);
    QNetworkRequest request(cUrl);
    request.setRawHeader(keyHeader.c_str(), param1.c_str());

    connect(pManager_, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
    connect(pManager_, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError> & )), this,
            SLOT(handleSslErrors(QNetworkReply*, const QList<QSslError> & )));

    cUrl.addQueryItem("name", QString::fromStdString(param2));

    pManager_->get(request); // memory corruption error encountered in main application after calling this

    std::cout << "after calling get" << std::endl;
}

void HttpRequest::requestFinished(QNetworkReply *pReply)
{
    QByteArray responseData;

    std::cout << " request finished" << std::endl;
    int responseStatus = pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    std::cout << " status code: " << responseStatus << std::endl;

    if(pReply->error())
        std::cout << " Error: " << pReply->errorString().toStdString() << std::endl;
    else
    {
        responseData = pReply->readAll();
        qDebug() << " Response data: " << responseData; 
        const char* pResponseData = responseData.data(); 
       qDebug() << "pResponseData: " << pResponseData ; 
        
        // parsing here
    }

    pReply->deleteLater();
    pManager_->deleteLater();
}

void HttpRequest::handleSslErrors(QNetworkReply *pReply, const QList<QSslError> & )
{
    std::cout << " SSL ERROR" << std::endl;
    int responseStatus = pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
}

主要的 GUI 应用程序:

DialogTest::DialogTest(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogTest)
{

    // some codes here
    
    if(enabled)
    {
        HttpRequest::instance()->getInformation(param1, param2); // memory corruption happened here when I called getRequest() method with no event loop
    }
    
    // other threads here
}

下面是使用 QEventLoop 的代码:

void HttpRequest::getRequest(string param1, string param2)
{
    QTimer qTimer;
    QEventLoop loop;
    
    pManager_ = new QNetworkAccessManager(this);

    QUrl cUrl(sampleUrl);
    QNetworkRequest request(cUrl);
    request.setRawHeader(keyHeader.c_str(), param1.c_str());
   
    connect(&qTimer,SIGNAL(timeout()),&loop, SLOT(quit()));
    connect(pManager_, SIGNAL(finished(QNetworkReply*)),&loop, SLOT(quit()));

    QNetworkReply *pReply = pManager_->get(request);

    qTimer.start(1000);
    loop.exec();

    int responseCode = pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    std::cout << "status code: " << responseCode << std::endl;

    if(pReply->error())
    {
        std::cout << " Error: " << pReply->errorString().toStdString() << std::endl;
    }
    else
    {
        qDebug() << "[HttpRequest] Response data: " << pReply->readAll();
        QByteArray response = pReply->readAll(); // it printed this value: "{"count":3,"codes":["x00000A","x00000B","x00000C"]}" which is correct
        char* pResponseData = response.data(); 
       qDebug() << "pResponseData: " << pResponseData ; //it printed this: pResponseData:
    }

    delete pReply;
    delete pManager_;
}

我期待来自 HTTP 获取命令的响应数据:"{"count":3,"codes":["x00000A","x00000B","x00000C"]}"

问题:实现这一点的最佳方法是什么?我想将所有 HTTP 请求放在一个库中,然后用 GUI 将其称为我的主应用程序。请注意:

  • 当我在库中使用 QEventLoop 等待响应时, QNetworkReply.data() 为空。我需要 QNetworkReply.data() 的值进行解析。
  • 当我没有使用 QEventLoop 并且单独使用信号和槽时(如上面的代码所示),在执行 HTTP get 命令后,主应用程序发生内存损坏。没有收到响应数据。
4

1 回答 1

0

一个建议:

永远不要对 QObject 使用直接删除。坏的:

delete pReply;
delete pManager_;

Qt方式,好:

pReply->deleteLater();
pManager->deleteLater();

更好:没有“新”(动态内存)

QNetworkAccessManager Manager_;
...
connect(&Manager_, SIGNAL(finished(QNetworkReply*)),&loop, SLOT(quit()));
 
..
pReply->deleteLater();
于 2020-10-14T14:20:56.267 回答