【问题标题】:Receiving unhandled NULL exception when using curl_easy_cleanup(curl)使用 curl_easy_cleanup(curl) 时收到未处理的 NULL 异常
【发布时间】:2018-09-25 17:59:27
【问题描述】:

我有一个 Unity 应用程序,它使用我编写的用于发出 http 请求的 c++ 插件。这个插件使用 curl 库。

根据 curl 文档,他们建议使用 curl_easy_cleanup(curl) 作为最后一个命令,以释放句柄并清理所有资源。

这是我发出一个简单的 http POST 请求的代码:

struct curl_slist *headers = NULL;
CURL *curl = NULL;
curl  = curl_easy_init();
int httpCode(0);

if(curl)
{
    headers = curl_slist_append(headers, "Accept: application/json");
    headers = curl_slist_append(headers, "Content-Type: application/json");
    headers = curl_slist_append(headers, "charsets: utf-8");

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
    //Set remote URL
    curl_easy_setopt(curl, CURLOPT_URL, endpoint.c_str());

    std::string params;
    for(std::unordered_map<std::string,std::string>::iterator it = parameters.begin(); it != parameters.end(); ++it)
    {
        params = it->first + ": " + it->second + " ";
        headers = curl_slist_append(headers, (const char *)params.c_str());
    }

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonObject.c_str());
    CURLcode res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ResponseRecievedCallback);

    res = curl_easy_perform(curl);
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);

//Uncommenting this line makes my application crash

    //curl_easy_cleanup(curl);
}

但是,当我添加此行时,我的统一应用程序崩溃并出现以下异常:

 Receiving unhandled NULL exception
Obtained 13 stack frames.
#0  0x000001207a4438 in Curl_expire_clear
#1  0x00000120790e25 in Curl_close

过去几天我一直在网上寻找一些解决方案,阅读 curl 文档但找不到任何帮助。如果有人能解释为什么这可能会崩溃,我将不胜感激?

非常感谢!

【问题讨论】:

  • 我要问一个可能完全不相关的问题:为什么要投 (const char *)params.c_str() ?只是好奇应该解决什么问题。
  • 这只是我使用字符串的一个选择,因此我可以轻松地与 + 运算符连接以附加参数。但这与我面临的问题完全无关。
  • Curl_expire_clear() 只处理在多模式下使用的句柄,否则它会立即返回。在调试器中运行您的程序(当然,使用启用调试的 libcurl)将有助于缩小范围,就像显示您的 ResponseReceivedCallback 一样,但闻起来就像您在某个地方有缓冲区溢出,覆盖了 curl 结构的一部分。也许也可以尝试通过 valgrind 运行它?
  • 感谢您的回复! ResponseReceivedCallback 所做的只是将传入的 json 响应保存为字符串: std::size_t ResponseRecievedCallback(const char* in, size_t size, size_t num, std::string* out) { const std::size_t totalBytes(size * num) ;响应字符串 = 在;返回总字节数;我从未使用过 Valgrind,感谢您的指点!让我看看。
  • 仅供参考,response_string = in; 是错误的。 num 的存在是有原因的。不能保证接收到的请求会被终止(除非您还发送了终止符,即使那样,它也可能会出现在单独的帧中)。你应该使用std::string response_string(in, in+num);

标签: c++ curl libcurl


【解决方案1】:

通过将 httpCode 设为 long 而不是 int(根据文档)解决了该问题。我之前一定忽略了它!

【讨论】:

    猜你喜欢
    • 2015-12-11
    • 2022-09-28
    • 1970-01-01
    • 2021-11-28
    • 2015-12-20
    • 2019-11-24
    • 2019-10-01
    • 2013-10-21
    • 1970-01-01
    相关资源
    最近更新 更多