Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

valgrind detects memory leaks when using libcurl (no ssl)

In my C program I use some basic functions of libcurl. Today I ran valgrind in order to check if I have memory leaks and valgrind went crazy reporting multiple errors.

I tracked it basically down to:

CURL *curl;
CURLcode res;

curl = curl_easy_init();
// ...
curl_easy_cleanup(curl);

If I remove the code that uses libcurl completely, valgrind doesnt report any errors.

I already read that there are some problems using valgrind with libcurl and ssl, but I dont fetch any https urls or the like.

What can I do? Can I make valgrind shut up about libcurl errors (possible false positives?) and report only errors from my code? Due to the huge amount of errors despite most simple usage of libcurl the output of valgrind is quite confusing.

Unfortunately I dont have a debug built of libcurl installed, so valgrind doesnt even report the line numbers/files where it deteced the leaks. The error messages look like:

==27330== 
==27330== HEAP SUMMARY:
==27330==     in use at exit: 34,960 bytes in 2,406 blocks
==27330==   total heap usage: 20,130 allocs, 17,724 frees, 2,511,576 bytes allocated
==27330== 
==27330== 40 (20 direct, 20 indirect) bytes in 1 blocks are definitely lost in loss record 383 of 445
==27330==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==27330==    by 0x4B173FD: ???
==27330==    by 0x4B17A8B: ???
==27330==    by 0x4B84957: ???
==27330==    by 0x4B849FD: ???
==27330==    by 0x4B72814: ???
==27330==    by 0x4B734C1: ???
==27330==    by 0x4B78DE2: ???
==27330==    by 0x4B7524B: ???
==27330==    by 0x49B2F76: ???
==27330==    by 0x49C9ECB: ???
==27330==    by 0x49BC96A: ???
...
like image 791
Max Avatar asked Dec 09 '22 05:12

Max


1 Answers

If you're starting with the first libcurl example (simple.c), they don't call curl_global_init(long flags) and curl_global_cleanup() at the end, and valgrind will report potential issues. As stated in the libcurl docs, you MUST call BOTH curl_global_init and curl_global_cleanup. I verified myself that this solves the problem; valgrind will report that all heap blocks were freed.

like image 169
veegee Avatar answered Jan 02 '23 16:01

veegee