[asterisk-dev] Memory leak when use ast_set_read_format

Mark Michelson mmichelson at digium.com
Wed May 26 09:39:28 CDT 2010


On 05/26/2010 02:12 AM, MohammedShehzad wrote:
>
>
> On Wed, May 26, 2010 at 1:11 AM, Mark Michelson wrote:
>
>     On 05/25/2010 08:18 AM, MohammedShehzad wrote:
>     >
>     > Hi all,
>     >
>     > My custom IVR application of asterisk records the voice of user.
>     > It sets the read format to sleaner using ast_set_read_format
>     funciton
>     > on line number  2144.
>     >         flag = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
>     > Which is leaving behind some leaks : below is the part of the
>     trace of
>     > asterisk under run valgrind as below:
>     >
>     > ==11972==
>     > ==11972== 52,512 bytes in 3 blocks are definitely lost in loss
>     record
>     > 115 of 117
>     > ==11972==    at 0x4A04B32: calloc (vg_replace_malloc.c:279)
>     > ==11972==    by 0x4A601D: newpvt (utils.h:359)
>     > ==11972==    by 0x4A714F: ast_translator_build_path
>     (translate.c:288)
>     > ==11972==    by 0x438270: set_format (channel.c:2778)
>     > ==11972==    by 0x162A19A8: ??? (app_custom_ivr.c:2144)
>     > ==11972==    by 0x1629FB83: ??? (app_custom_ivr.c:1099)
>     > ==11972==    by 0x1629EB25: ??? (app_custom_ivr.c:525)
>     > ==11972==    by 0x47F21C: pbx_extension_helper (pbx.c:537)
>     > ==11972==    by 0x4814E0: __ast_pbx_run (pbx.c:2317)
>     > ==11972==    by 0x482158: pbx_thread (pbx.c:2634)
>     > ==11972==    by 0x4AB3EB: dummy_start (utils.c:865)
>     > ==11972==    by 0x376EC06616: start_thread (in
>     > /lib64/libpthread-2.5.so <http://libpthread-2.5.so>
>     <http://libpthread-2.5.so>)
>     > ==11972==    by 0x376E4D3C2C: clone (in /lib64/libc-2.5.so
>     <http://libc-2.5.so>
>     > <http://libc-2.5.so>)
>     > ==11972==
>     >
>     > I do reset the read format back to old read format, but even
>     though it
>     > is showing leaks.
>     >
>     > Any one can please let me know what i am missing in using
>     > ast_set_read_format function?
>     >
>     >
>     > --
>     >
>     > -MohammedShehzad
>
>     ast_set_read_format will allocate memory if it needs to create a
>     translation path. However, when you call ast_set_read_format, the old
>     translation path on the channel will be freed first. Furthermore,
>     when a
>     channel is destroyed, the translation path is freed as well.
>
>     When are you seeing this output from valgrind? Are you stopping
>     Asterisk
>     with channels still up? if that is the case, then it is not surprising
>     to see that there is still memory allocated for translation paths.
>
>     Mark Michelson
>
> I assume that there is no need to call any function to destroy the 
> channel implicitly.
> I get this output after making few calls which hits the custom IVR. 
> Although I stop the asterisk only after making sure that no channel is 
> active.
>
> -- 
>
> -MohammedShehzad

Even though no channels may be active, you may have introduced a channel 
reference leak in your code (assuming you are using Asterisk trunk 
here). If you ever call a function like ast_channel_get_*, then you need 
to make sure to call ast_channel_unref on that channel once you are 
finished with it. If you issue the CLI command "core show channels" 
after all channels are inactive and still see channels listed, this may 
be your problem.

Mark Michelson



More information about the asterisk-dev mailing list