cURL / Mailing Lists / curl-library / Single Mail

curl-library

correct usage of curl_multi_setopt(_multi, CURLMOPT_SOCKETFUNCTION, _sock_cb); / curl_multi_remove_handle / curl_easy_cleanup

From: svante karlsson <saka_at_csi.se>
Date: Mon, 13 Jan 2014 23:08:53 +0100

I've combined boost asio with libcurl to build a async http client library.
However I'm thinking I might have misunderstood the callback sequence or
the usage pattern.

int http_client_async_handler::sock_cb(CURL *e, curl_socket_t s, int what,
void* per_socket_user_data)
{
...
switch (what)
{
case CURL_POLL_REMOVE:

                async_context* context=NULL;
curl_easy_getinfo(e, CURLINFO_PRIVATE, &context);
long http_result = 0;
curl_easy_getinfo(e, CURLINFO_RESPONSE_CODE, &http_result);
curl_multi_remove_handle(_multi, e); // IF I CALL THIS THEN WE CRASH IN
LIBCURL.... (A)
....
delete context; // WILL CALL
                                      // if (context->easy)
                                      //
 curl_easy_cleanup(context->easy);
      (B)
                                      //
               }
...
}

I start this using.

_multi = curl_multi_init();
curl_multi_setopt(_multi, CURLMOPT_SOCKETFUNCTION, _sock_cb);

....

My problem seems to be that if I use A curl crashes.
if I use B then I get a stackoverflow when
curl_easy_cleanup->Curl_close->curl_multi_remove_handle>
.......curl_easy_cleanup....

  csi_http_client_async_sample.exe!curl_easy_cleanup(void * curl) Line 886 C
  csi_http_client_async_sample.exe!csi::http_client_async_handler::async_context::~async_context()
Line 39 C++
  csi_http_client_async_sample.exe!csi::http_client_async_handler::async_context::`scalar
deleting destructor'(unsigned int) C++
  csi_http_client_async_sample.exe!csi::http_client_async_handler::sock_cb(void
* e, unsigned __int64 s, int what, void * per_socket_user_data) Line 194 C++
  csi_http_client_async_sample.exe!csi::http_client_async_handler::_sock_cb(void
* e, unsigned __int64 s, int what, void * user_data, void *
per_socket_user_data) Line 146 C++
  csi_http_client_async_sample.exe!singlesocket(Curl_multi * multi,
SessionHandle * data) Line 2013 C
  csi_http_client_async_sample.exe!curl_multi_remove_handle(void *
multi_handle, void * curl_handle) Line 563 C
  csi_http_client_async_sample.exe!Curl_close(SessionHandle * data) Line 397
C
  csi_http_client_async_sample.exe!curl_easy_cleanup(void * curl) Line 886 C

When I wrote the code I did the callbacks by trial and error but I'll have
an obvious memory leak if I use the only thing that is working for me (
just skipping the curl_easy_cleanup in the destructor )

As I see it I could remove the callback mechanism first and maybe get rid
of the stack overflow but it feels like I'm missing something here. How is
this supposed to be working?

curl version 7.33.0, windows 64

/svante

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2014-01-13