UDF thread state

11 views
Skip to first unread message

kjkeefe

unread,
Feb 10, 2008, 12:39:25 AM2/10/08
to The UDF Repository for MySQL
I've written a UDF that fetches an XML file from a webserver using
libcurl. The UDF appears to be working well. However, after I run it,
the thread doesn't appear to terminate. When I do "show full
processlist", the function calls are still in the process table and
the Command is "Query" and the state says "freeing items".

Does this means my udf is getting stuck somewhere?

I really wish there was a way to be sure that my udf's aren't leaking
any memory... I've triple checked my code to make sure that whatever
I've malloced is freed, but I can't be sure...

kjkeefe

unread,
Feb 10, 2008, 1:08:24 AM2/10/08
to The UDF Repository for MySQL
Hmm, I think I may have found the problem... However, I am not sure
one of my vars are getting freed though.

I malloc a string to store the result from the curl request. I return
that string from my udf function like I'm are supposed to. However, I
store my malloc'd string in initid->ptr so that I can free it in my
deinit funciton. That free seems to be causing the problem. When I
comment it out, the thread ends like I'd expect it to...

CurlResults are defined like so:

struct CurlResult {
char *value;
size_t size;
};

Here is the end of my udf main function:

if (curlResult.value) {
initid->ptr = (void *)&(curlResult.value);
return curlResult.value;
}
return "No result returned";

And, here is the entire body of my deinit function with the problem
lines commented out:

/*if (initid->ptr)
free(initid->ptr);*/

Any help on this would be greatly appreciated...

Thanks,
Ken

Roland Bouman

unread,
Feb 10, 2008, 5:54:56 AM2/10/08
to The UDF Repository for MySQL
Hi!

I have used libcurl in UDFs without these issues. That said...I can't
recall this CurlResult thingie...probably my ignorance with regard to
libcurl.

What I do is set a data handler to store the result.

curl_easy_setopt(
udf_curl_http->curl
, CURLOPT_WRITEFUNCTION
, _udf_curl_write_data
);

so _udf_curl_write_data is called whenever there is data that libcurl
has fetched and wants to offload it to the app (in this case url).
_udf_curl_write_data is a function you write yourself. That gets a
handle passed that you can use to store the data, and I define that
myself too:

curl_easy_setopt(
udf_curl_http->curl
, CURLOPT_WRITEDATA
, &(udf_curl_http->data)
);

So each time the write data function is called, it will get a pointer
tp udf_curl_http->data passed.

For more details, take a look at my code in lib_mysqludf_curl.tar.gz
in the files section. You can compile it using the makefile in the
Debug directory, and you will most likely need to adjust the paths to
the MySQL include directory and to the location of the libcurl shared
library to match your system

Roland Bouman

unread,
Feb 10, 2008, 6:04:44 AM2/10/08
to The UDF Repository for MySQL
Mmm, for some reason google groups won't let me up load the
file....and just to prove murphy's law the mysqludf.org site tools
area is unavailable too ;(


Sending you Private mail...
Reply all
Reply to author
Forward
0 new messages