NEED HELP URGENTLY! udf crashes when multiple queries are made simultaneously

13 views
Skip to first unread message

Tariq

unread,
Feb 26, 2008, 6:59:05 AM2/26/08
to The UDF Repository for MySQL
Hello,

I am a new member on this forum. I have recently started using mysql
udf. The code that i am implementing requires me to get the value of
input parameters from 'init' function and use them to fill an array
based on the input parameters in 'clear' function. Now the problem is
that 'clear' function does not provide UDF_ARGS *args arguments, so
what I am doing is that creating a global pointer variable and
assigning the value of UDF_ARGS *args to that global variable in
'init' function. Now I can use this global variable to access input
parameters even in 'clear' function. This works fine untill i dispatch
multiple queries simultaneously. I mean it crashes when multiple users
are making queries on the server and some queries are made
simultaneously. The logic of the functionality im building requires me
to get UDF_ARGS *args parameters in 'clear' function. Please help me
in this regard. I will appreciate it very much. I am not sure where to
post the code, so im just writing some part of it below so that u
understand what i m doing.

UDF_ARGS * clear_args;

my_bool rank_init( UDF_INIT *initid, UDF_ARGS *args, char *message )
{
my_bool status;
int nArg = 4;
clear_args = args; // HERE I M ASSIGNING args TO THE GLOBAL
VARIABLE

if( !( initid->ptr = malloc( sizeof( RANKER ) ) ) )
{
......................
clear_args = NULL;
status = 1;
}
else
{
clear_args = args;
...........................
status = 0;
}

return status;
}

char* rank_clear( UDF_INIT *initid, char *is_null, char *error )
{
RANKER * p_ranker = ( RANKER * ) initid->ptr;

if( p_ranker != NULL )
{
..................

// HERE I M JUST USING THE GLOBAL VARIABLE TO
FILL SOME ARRAYS
for( nArg = 4 ; nArg < clear_args->arg_count ; nArg+=3 )
{
p_ranker->map[ nArg ] = 0.0;
if( strncmp( clear_args->args[ nArg + 2 ], " ", 1 ) == 0 )
p_ranker->sig[ nArg ] = 2.0;
else if( strncmp( clear_args->args[ nArg + 2 ], "+", 1 ) == 0 )
p_ranker->sig[ nArg ] = 1.0;
else if( strncmp( clear_args->args[ nArg + 2 ], "-", 1 ) == 0 )
p_ranker->sig[ nArg ] = 0.0;
}
}
*is_null = 0;
}
else
*is_null = 1;

return 0;
}

Roland Bouman

unread,
Feb 26, 2008, 2:15:13 PM2/26/08
to The UDF Repository for MySQL
Hi Tariq,


> I am a new member on this forum. I have recently started using mysql

Welcome!

> udf. The code that i am implementing requires me to get the value of
> input parameters from 'init' function and use them to fill an array
> based on the input parameters in 'clear' function. Now the problem is
> that 'clear' function does not provide UDF_ARGS *args arguments, so
> what I am doing is that creating a global pointer variable and
> assigning the value of UDF_ARGS *args to that global variable in
> 'init' function. Now I can use this global variable to access input

This is asking for trouble...You can only use static globals to hold
some immutable data. Or at the very least you must prevent at all cost
multiple threads accessing those globals (locking)

In short, this is not the way to go.

THe correct way doing this is to use the ptr member of the UDF_INITID
struct, like so:

initid->ptr = malloc(numbytes);

FOr most functions it is ok to alloc mem just once in the INIT
function, but nothing prevents you from doing it whenever and in
whatever function you like. Note that the UDF_INITID struct is passed
to all functions that make up the UDF interface.

Deallocating memory should be done in the XXX_deinit function.

Note that if you alloc mem in the XXX_init function and accidentally
have an error there, the XXX_deinit function will *not* be called -
meaning you will have to clean up there too.

kind regards,

I hope it helps.
Reply all
Reply to author
Forward
0 new messages