cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Infinite loop when calling curl_multi_cleanup

From: Ray Satiro via curl-library <curl-library_at_cool.haxx.se>
Date: Tue, 18 Oct 2016 15:46:36 -0400

On 10/18/2016 12:08 PM, Miloš Ljumović wrote:
> // Here I have to use dynamic memory - strange, cause I saw
> "strdup" in libcurl
> char* szUrlBuffer = new char[1024]; // leak, needs upgrade
> StringCchPrintf(szUrlBuffer, 1024, "%s%s", szAPNSAddress,
> szDeviceToken);
> curl_easy_setopt(hEasyHandle, CURLOPT_URL, szUrlBuffer);
> //----------------- !!!!!!!!
> curl_easy_setopt(hEasyHandle, CURLOPT_HTTP_VERSION,
> CURL_HTTP_VERSION_2_0);
> curl_easy_setopt(hEasyHandle, CURLOPT_SSLCERT, ...);
> ...
> curl_easy_setopt(hEasyHandle, CURLOPT_USERAGENT, "curl/7.51.0-DEV");
> curl_easy_setopt(hEasyHandle, CURLOPT_TCP_KEEPALIVE, 1L);
> curl_easy_setopt(hEasyHandle, CURLOPT_PIPEWAIT, 1L);
>
> Lock();
> {
> curl_multi_add_handle(hMultiHandle, hEasyHandle);
> }
> Unlock();

Your example is not complete, but based on what you have provided here
is what you should look at:

"Strings passed to libcurl as 'char *' arguments, are copied by the
library" [1] with the exception of CURLOPT_POSTFIELDS.

If your program is crashing when you delete the buffer after setting it
as the URL then the problem is elsewhere.

"Handles. You must never share the same handle in multiple threads. You
can pass the handles around among threads, but you must never use a
single handle from more than one thread at any given time." [2]

It would appear that though you are locking with a critical section when
you add and remove multi handles you are not locking around other calls
where the multi is in use. Adding a critical section around every multi
call in your perform thread is inefficient and will lead to more waiting
than necessary in the threads that are adding. The way I would do it is
create a thread-safe queue of prepared easy handles and then
multi_add_handle in the thread that is using the multi handle.

Also, I notice you didn't give your curl_version() information. libcurl
is continuously improving, and so is nghttp2. I would try the latest of
both.

If you still have problems after accounting for these things please put
together a minimal self-contained example that compiles and reproduces
the problem.

[1]: https://curl.haxx.se/libcurl/c/curl_easy_setopt.html
[2]: https://curl.haxx.se/libcurl/c/threadsafe.html

-------------------------------------------------------------------
List admin: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2016-10-18