cURL / Mailing Lists / curl-library / Single Mail


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

From: svante karlsson <>
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)

                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
delete context; // WILL CALL
                                      // if (context->easy)

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

  csi_http_client_async_sample.exe!curl_easy_cleanup(void * curl) Line 886 C
Line 39 C++
deleting destructor'(unsigned int) C++
* e, unsigned __int64 s, int what, void * per_socket_user_data) Line 194 C++
* 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
  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


List admin:
Received on 2014-01-13