libcurl中的以下函数保存文件并返回http状态代码。但是,当我使用valgrind运行它时,它报告0字节“绝对丢失”,“间接丢失”,“可能丢失”,但它报告 47448字节 “仍然可以访问“即可。我正在尝试解决“仍然可以访问”的字节。
下面的代码中是否存在潜在的内存泄漏?
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream){
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
void connectAndSaveFile(char* url, char* output_file_name){
CURL *curl;
curl = curl_easy_init();
if (curl) {
FILE *fp = fopen(output_file_name,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
}
string get_http_status_code(string URL) {
CURL *session;
session = curl_easy_init();
curl_easy_setopt(session, CURLOPT_URL, URL.c_str());
curl_easy_setopt(session, CURLOPT_NOBODY, true);
CURLcode curl_code = curl_easy_perform (session);
long http_code = 0;
curl_easy_getinfo (session, CURLINFO_RESPONSE_CODE, &http_code);
curl_easy_cleanup(session);
std::ostringstream buff;
buff << http_code;
return buff.str();
}
答案 0 :(得分:1)
&#34;仍然可以访问&#34;最常见的不是泄漏
答案 1 :(得分:0)