我正在开发一个Web服务,并且在C ++中有一个curl的小问题。
以下代码

string WSUser::getUser(int id){
CURL *curl;
CURLcode res;
if(curl == NULL) curl = curl_easy_init();
if(curl) {
    ostringstream oss;
    curl_easy_setopt(curl, CURLOPT_URL, http://"example.com");
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    res = curl_easy_perform(curl);

    curl_easy_cleanup(curl);

    if(res != CURLE_OK) return curl_easy_strerror(res);
}


工作正常,但问题是,当我再次调用此方法时

WSUser *wsUser = new WSUser();
cout << wsUser->getUser(1) << endl;
cout << wsUser->getUser(2) << endl;


然后我得到一个错误:


  WebService.exe中的异常地址0x54ba7e2c(msvcr100d.dll):0xC0000005:Zugriffsverletzung beim Lesen位置0xfeeefee8。

最佳答案

这里

CURL *curl;
CURLcode res;
if(curl == NULL) curl = curl_easy_init();


由于您没有初始化curl,因此它具有不确定的值,并且使用它(即将其与NULL进行比较)是不确定的。
实际上,最有可能发生的情况是您会或多或少地随机调用curl_easy_init,而在curl中保留一个随机值。
您只是很不幸,在第一个电话没有崩溃的时候,它恰好是零。

你要

CURL* curl = curl_easy_init();

关于c++ - (C++)两次调用curl方法给出错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23384822/

10-12 16:16
查看更多