[asterisk-dev] Newbie question about ast_pthread_create_background

Tilghman Lesher tlesher at digium.com
Sat Sep 12 10:10:40 CDT 2009


On Saturday 12 September 2009 05:35:54 Alex Massover wrote:
> The only concern I still have is about using pbx_builtin_getvar_helper().
> Is it always safe to do: char * my_val= pbx_builtin_getvar_helper(...);
> or I need to do:
> char * my_val = ast_strdup(pbx_builtin_getvar_helper(...));
>
> I see that my_val still exists inside my detached thread, even after the
> channel hangup. But is it always safe? When actually the memory that
> pbx_builtin_getvar_helper() points to freed?

Actually, you should wrap all pbx_builtin_getvar_helper() with
ast_channel_lock/ast_channel_unlock(), to be sure that there's no race
condition between the time that you retrieve the pointer to the variable and
the time that you copy the variable's value.  Of course, if you're just doing
something else with the variable in the short term, it's much cheaper just to
do that within the lock.

For example, instead of doing:
ast_channel_lock(chan);
dup_foo = ast_strdup(pbx_builtin_getvar_helper("whatever"));
ast_channel_unlock(chan);
val_foo = atoi(dup_foo);
ast_free(dup_foo);

do:
ast_channel_lock(chan);
val_foo = atoi(pbx_builtin_getvar_helper("whatever"));
ast_channel_unlock(chan);

-- 
Tilghman Lesher
Digium, Inc. | Senior Software Developer
twitter: Corydon76 | IRC: Corydon76-dig (Freenode)
Check us out at: www.digium.com & www.asterisk.org



More information about the asterisk-dev mailing list