<br><br><div class="gmail_quote">On Tue, Jan 5, 2010 at 9:27 PM, Russell Bryant <span dir="ltr">&lt;<a href="mailto:russell@digium.com">russell@digium.com</a>&gt;</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>
&gt; Hello every body,<br>
&gt;<br>
&gt; I am facing some problems in some custom changes in rtp.c file in which have<br>
&gt; used some threading functions.<br>
&gt; I create a thread in ast_rtp_new_with_bindaddr function as below:<br>
&gt;<br>
&gt;              pthread_attr_t attr;<br>
&gt;              rtp-&gt;thread_var=NULL;<br>
&gt;              pthread_attr_init(&amp;attr);<br>
&gt;              ast_pthread_create_background(&amp;rtp-&gt;thread_var,&amp;attr,<br>
&gt; somecustom_thread,(void *)rtp);<br>
&gt;              pthread_attr_destroy(&amp;attr);<br>
&gt;<br>
&gt; Thread of &quot;somecustom_thread&quot; is being created, which continue in while loop<br>
&gt; (i.e. while(1))<br>
&gt; I destroy this thread in ast_rtp_stop function with below line:<br>
&gt; pthread_cancel(rtp-&gt;thread_var);<br>
&gt; *<br>
&gt; *My function works very fine in normal one-two calls. But when I put the<br>
&gt; modified asterisk in regression/stress testing (test with many calls of<br>
&gt; 20-30 second duration for long run), I finds the memory leaks as below under<br>
&gt; Valgrind:<br>
&gt; ==19707== 98,320 bytes in 683 blocks are possibly lost in loss record 39 of<br>
&gt; 41<br>
&gt; ==19707==    at 0x4004864: calloc (vg_replace_malloc.c:279)<br>
&gt; ==19707==    by 0xB09109: _dl_allocate_tls (in /lib/<a href="http://ld-2.7.so" target="_blank">ld-2.7.so</a>)<br>
&gt; ==19707==    by 0xCA9BFE: pthread_create@@GLIBC_2.1 (in /lib/<br>
&gt; <a href="http://libpthread-2.7.so" target="_blank">libpthread-2.7.so</a>)<br>
&gt; ==19707==    by 0x812276E: ast_pthread_create_stack (utils.c:910)<br>
&gt; ==19707==    by 0x80EE60A: ast_rtp_new_with_bindaddr (rtp.c:2124)<br>
&gt; ==19707==    by 0x4AD8D0E: sip_alloc (chan_sip.c:4677)<br>
&gt; ==19707==    by 0x4ADA318: find_call (chan_sip.c:4848)<br>
&gt; ==19707==    by 0x4B152DA: sipsock_read (chan_sip.c:16485)<br>
&gt; ==19707==    by 0x80C11B9: ast_io_wait (io.c:279)<br>
&gt; ==19707==    by 0x4B164D6: do_monitor (chan_sip.c:16772)<br>
&gt; ==19707==    by 0x8122526: dummy_start (utils.c:856)<br>
&gt; ==19707==    by 0xCA950A: start_thread (in /lib/<a href="http://libpthread-2.7.so" target="_blank">libpthread-2.7.so</a>)<br>
&gt;<br>
&gt; *<br>
&gt; *<br>
&gt; This seems to be a problem with pthread library, but it only occurs which I<br>
&gt; do regression testing.<br>
&gt; My question is that what is the mistake in creating and destroying the<br>
&gt; thread.<br>
&gt; If the there is a mistake in my code of creating the thread, please let me<br>
&gt; know.<br>
&gt; If any body know better option to run the thread just after RTP media starts<br>
&gt; and stop the thread when RTP media stop, please point me into that<br>
&gt; direction.<br>
&gt;<br>
&gt; 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(&amp;attr, PTHREAD_CREATE_DETACHED);<br>
            ast_pthread_create(&amp;rtp-&gt;thread_var, &amp;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>