[asterisk-dev] Newbie question about ast_pthread_create_background

Alex Massover alex at jajah.com
Sat Sep 12 11:50:02 CDT 2009


Thank you, I'll wrap it.

--
Best Regards,
Alex Massover

-----Original Message-----
From: asterisk-dev-bounces at lists.digium.com [mailto:asterisk-dev-bounces at lists.digium.com] On Behalf Of Tilghman Lesher
Sent: Saturday, September 12, 2009 6:11 PM
To: Asterisk Developers Mailing List
Subject: Re: [asterisk-dev] Newbie question about ast_pthread_create_background

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

_______________________________________________
--Bandwidth and Colocation Provided by http://www.api-digital.com--

AstriCon 2009 - October 13 - 15 Phoenix, Arizona
Register Now: http://www.astricon.net

asterisk-dev mailing list
To UNSUBSCRIBE or update options visit:
   http://lists.digium.com/mailman/listinfo/asterisk-dev

This mail was received via Mail-SeCure System.



This mail was sent via Mail-SeCure System.





More information about the asterisk-dev mailing list