I am using libcurl I have my downloading of files inside of a class, to which I want to see a progress function. I notice I can set a typical function pointer by
curl_easy_setopt(mCurl, CURLOPT_PROGRESSFUNCTION, progress_func3);
However, I would like to set it to a function pointer to my class. I can get the code to compile with
curl_easy_setopt(mCurl, CURLOPT_PROGRESSFUNCTION, &MyClass::progress_func3);
and the progress_func3
function will get called. The problem is, as soon as it returns there will be a "Buffer overrun detected!" error through, saying the program can not safely continue and execute, and must be terminated. (It is a Microsoft Visual C++ Runtime Library error window, I am using Visual Studio 2010).
When I use a function, there is no problem, but when I use a member function pointer, I will get this error. How can I use a member function pointer in libcurl?
You can this using boost::bind(). For example:
is a pointer to a method that returns void and has no arguments. If your callback needs arguments, use placeholders as follows:
The
this
pointer can be replaced with a point to an instance ofMyClass
.EDIT: You should be able to use boost::function<> and function ptr types relatively interchangeable. For example:
is equivalent to
You may have to layer it with a function in between to make the compiler happy. I know that I've defined a callback using boost::function<> and passed in a regular function pointer.
A non-static member function needs a
this
pointer to be callable. You can't provide thatthis
pointer with this type of interface, so using a non-static member function is not possible.You should create a "plain C" function as your callback, and have that function call the member function on the appropriate
MyClass
instance.Try something like:
Then:
(You're responsible for the type match here, obviously. If you attach anything else but a
MyClass
pointer to the progress data, you're on your own.)