<br><br><div class="gmail_quote">On Tue, Jan 5, 2010 at 9:27 PM, Russell Bryant <span dir="ltr"><<a href="mailto:russell@digium.com">russell@digium.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="h5">On 01/05/2010 12:31 AM, MohammedShehzad wrote:<br>
> Hello every body,<br>
><br>
> I am facing some problems in some custom changes in rtp.c file in which have<br>
> used some threading functions.<br>
> I create a thread in ast_rtp_new_with_bindaddr function as below:<br>
><br>
> pthread_attr_t attr;<br>
> rtp->thread_var=NULL;<br>
> pthread_attr_init(&attr);<br>
> ast_pthread_create_background(&rtp->thread_var,&attr,<br>
> somecustom_thread,(void *)rtp);<br>
> pthread_attr_destroy(&attr);<br>
><br>
> Thread of "somecustom_thread" is being created, which continue in while loop<br>
> (i.e. while(1))<br>
> I destroy this thread in ast_rtp_stop function with below line:<br>
> pthread_cancel(rtp->thread_var);<br>
> *<br>
> *My function works very fine in normal one-two calls. But when I put the<br>
> modified asterisk in regression/stress testing (test with many calls of<br>
> 20-30 second duration for long run), I finds the memory leaks as below under<br>
> Valgrind:<br>
> ==19707== 98,320 bytes in 683 blocks are possibly lost in loss record 39 of<br>
> 41<br>
> ==19707== at 0x4004864: calloc (vg_replace_malloc.c:279)<br>
> ==19707== by 0xB09109: _dl_allocate_tls (in /lib/<a href="http://ld-2.7.so" target="_blank">ld-2.7.so</a>)<br>
> ==19707== by 0xCA9BFE: pthread_create@@GLIBC_2.1 (in /lib/<br>
> <a href="http://libpthread-2.7.so" target="_blank">libpthread-2.7.so</a>)<br>
> ==19707== by 0x812276E: ast_pthread_create_stack (utils.c:910)<br>
> ==19707== by 0x80EE60A: ast_rtp_new_with_bindaddr (rtp.c:2124)<br>
> ==19707== by 0x4AD8D0E: sip_alloc (chan_sip.c:4677)<br>
> ==19707== by 0x4ADA318: find_call (chan_sip.c:4848)<br>
> ==19707== by 0x4B152DA: sipsock_read (chan_sip.c:16485)<br>
> ==19707== by 0x80C11B9: ast_io_wait (io.c:279)<br>
> ==19707== by 0x4B164D6: do_monitor (chan_sip.c:16772)<br>
> ==19707== by 0x8122526: dummy_start (utils.c:856)<br>
> ==19707== by 0xCA950A: start_thread (in /lib/<a href="http://libpthread-2.7.so" target="_blank">libpthread-2.7.so</a>)<br>
><br>
> *<br>
> *<br>
> This seems to be a problem with pthread library, but it only occurs which I<br>
> do regression testing.<br>
> My question is that what is the mistake in creating and destroying the<br>
> thread.<br>
> If the there is a mistake in my code of creating the thread, please let me<br>
> know.<br>
> If any body know better option to run the thread just after RTP media starts<br>
> and stop the thread when RTP media stop, please point me into that<br>
> direction.<br>
><br>
> Thanks in advance.<br>
<br>
</div></div>You should be creating your thread as detached or be running<br>
pthread_join() after you stop the thread you have created.<br>
<br>
--<br>
Russell Bryant<br>
Digium, Inc. | Engineering Manager, Open Source Software<br>
445 Jan Davis Drive NW - Huntsville, AL 35806 - USA<br>
<a href="http://www.digium.com" target="_blank">www.digium.com</a> -=- <a href="http://www.asterisk.org" target="_blank">www.asterisk.org</a> -=- <a href="http://blogs.asterisk.org" target="_blank">blogs.asterisk.org</a><br>
<br>
_______________________________________________<br>
--Bandwidth and Colocation Provided by <a href="http://www.api-digital.com--" target="_blank">http://www.api-digital.com--</a><br>
<br>
asterisk-dev mailing list<br>
To UNSUBSCRIBE or update options visit:<br>
<a href="http://lists.digium.com/mailman/listinfo/asterisk-dev" target="_blank">http://lists.digium.com/mailman/listinfo/asterisk-dev</a><br>
</blockquote></div><br>Hi,<br><br>Thanks Russell,<br>I made the thread detachable by replacing ast_pthread_create_background with below code:<br> pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);<br>
ast_pthread_create(&rtp->thread_var, &attr, ast_rtp_write_thread, rtp);<br><br>It resolved the above leak, it seems that there is another leak too as below, which did not get resolved:<br><br>==3112== 1,241,886 bytes in 2,849 blocks are still reachable in loss record 41 of 41<br>
==3112== at 0x4005525: malloc (vg_replace_malloc.c:149)<br>==3112== by 0x8075B0A: __ast_alloc_region (astmm.c:115)<br>==3112== by 0x8075AC5: __ast_calloc (astmm.c:198)<br>==3112== by 0x8120CFB: ast_threadstorage_get (threadstorage.h:130)<br>
==3112== by 0x8121961: ast_store_lock_info (utils.c:551)<br>==3112== by 0x806BA56: __ast_pthread_mutex_lock (lock.h:369)<br>==3112== by 0x806B97C: ast_register_file_version (asterisk.c:281)<br>==3112== by 0x812091D: __register_file_version (utils.c:28)<br>
==3112== by 0x814426A: (within /usr/sbin/asterisk)<br>==3112== by 0x8057A6C: (within /usr/sbin/asterisk)<br>==3112== by 0x8144138: __libc_csu_init (in /usr/sbin/asterisk)<br>==3112== by 0xB2D330: (below main) (in /lib/<a href="http://libc-2.7.so">libc-2.7.so</a>)<br>
==3112==<br><br>Thanks Russel for help.<br>--MohammedShehzad<br>