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: ???
...
I know this answer is coming a year later, but someone may still find it helpful.
After the call to curl_easy_cleanup(curl)
, try adding a call to curl_global_cleanup()
.
That worked for me.
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.
libcurl doesn't leak but it might use techniques that will alarm valgrind. So, to repeat from other answers, what are the errors that valgrind reports?
I don't expect you have the libcurl sources but, if you do, where do the valgrind errors point you?
What errors do you actually get?
And just as importantly - are the leaks a static amount, or do they grow over time? A small one-time static leak is far less important than something that is leaking as time goes on.
It is also possible this is a false positive from Valgrind; depends on the specific errors and where you see them.
Most likely valgrind is just getting things wrong concerning libcurl
. Often for such libraries it doesn't see one end of the allocation/deallocation correctly and gets confused. A good OS distribution should provide you with "suppression" files for this, but obviously yours didn't do that. You can deal with that yourself with the options --suppressions
and --gen-suppressions
or even put such things in a config file.
I asked on the mailing list and nobody could exactly tell me where my problem is, as I am still sture that I installed the debug version of the newest libcurl release, but still I couldnt see any debug symbols.
Anyhow, I set up a new virtual machine and compiled libcurl from source and the strange error messages disappeared.
A shame that I didnt find anything more useful though which might have helped others with the same problem…