If I implement a C callback like this:
register_callback([](){});
I get SIGSEGV when it starts, but if I register it as follows:
auto const f([](){}); register_callback(f);
Then it works great. Of particular interest (for me) is the stack trace created by the sanitizer:
ASAN:SIGSEGV ================================================================= ==22904==ERROR: AddressSanitizer: SEGV on unknown address 0x7f1582c54701 (pc 0x7f1582c54701 sp 0x7f1582c544a8 bp 0x7f1582c54510 T2)
It looks as if the function pointer was pointing to the stack. Does lambda push on stack to push code on stack? Since I am not committing anything, the location of the function pointer is a mystery to me. What's happening? Optimization flags were not used. I am not looking for workarounds.
EDIT: Apparently, “+” was the key to a working example. I do not know why this is necessary. Remove the "+" and the compilation example, but SIGSEGV will clang-3.5 with both clang-3.5 and gcc-4.9 .
#include <curl/curl.h> #include <ostream> #include <iostream> int main() { auto const curl(curl_easy_init()); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, "cnn.com"); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); /* auto const f([](char* const ptr, size_t const size, size_t const nmemb, void* const data) { *static_cast<::std::ostream*>(data) << ptr; return size * nmemb; } ); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, +f); */ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, +[](char* const ptr, size_t const size, size_t const nmemb, void* const data) { *static_cast<::std::ostream*>(data) << ptr; return size * nmemb; } ); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &::std::cout); curl_easy_perform(curl); curl_easy_cleanup(curl); } return 0; }