(c++)调用一个curl方法两次,给出并出错
(c++) Call a curl method twice gives and error
我正在开发一个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:0xC0000005:Zugriffsverletzung beim Lesen an Position 0xfeeefee8中取消绑定Ausnahme bei 0x54ba7e2c(msvcr100d.dll(。
此处为
CURL *curl;
CURLcode res;
if(curl == NULL) curl = curl_easy_init();
由于您没有初始化curl
,它有一个不确定的值,并且使用它(即将它与NULL
进行比较(是未定义的
在实践中最有可能发生的情况是,您或多或少会随机调用curl_easy_init
,在curl
中留下一个随机值。你只是运气不好,第一次打电话时正好有一个零,当时它没有崩溃。
你想要
CURL* curl = curl_easy_init();
相关文章:
- g++的分段错误(在NaN上使用to_string两次时)
- 蛇在C++不会连续转两次
- 检查一个数组是否包含在另一个数组中,以相反的顺序,至少两次
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 我应该如何去缓解两次出现的cin?
- Realloc 两次无法在 Visual Studio 上运行
- 使用 getline(cin, var) 两次在进行字符串比较时会产生错误 (==)
- 为什么映射插入和 map.find() 的单次迭代比插入和 map.find() 的两次单独迭代慢得多
- C++析构函数调用两次,堆栈分配的复合对象
- 为什么参数在构造 std::thread 时移动两次
- Qt插槽调用了两次
- 做 std::用相同的unique_ptr移动两次
- C++两次定义相同的函数会导致错误
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- 对于优化级别为 0 的 std::vector,析构函数被调用两次
- 使用柯南打包时如何避免列出两次依赖?
- 为什么要执行两次位移((x >> 4)<< 4)?
- 对结构向量进行两次排序
- 在同一 unix 套接字连接中读取元素两次时出错
- (c++)调用一个curl方法两次,给出并出错