<div dir="ltr"><div>Hello,</div><div><br></div><div>   I built one module similar to mod_niblebill for billing puroses. It works ok but sometimes I get a segfault and I don&#39;t understand why. The code I use in my application for generating outgoing session is:</div><div>     </div><div>------------------------------</div><div>status = switch_ivr_originate(session, &amp;peer_session, &amp;cause, cadena_marcado_tmp, 0, NULL, rt-&gt;callerid_name, rt-&gt;callerid_number, NULL, NULL, SOF_NONE, NULL);</div><div><br></div><div><span class="" style="white-space:pre">                </span>if(status == SWITCH_STATUS_SUCCESS){</div><div><br></div><div>      if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE)) {</div><div><span class="" style="white-space:pre">                                </span>switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, &quot;%s: Llamada switch_ivr_signal_bridge\n&quot;, rt-&gt;uuid);</div><div><span class="" style="white-space:pre">                                </span>switch_ivr_signal_bridge(session, peer_session);</div><div><span class="" style="white-space:pre">                        </span>} else {</div><div><span class="" style="white-space:pre">                                </span>switch_channel_t *channel = switch_core_session_get_channel(session);</div><div><span class="" style="white-space:pre">                                </span>switch_channel_t *peer_channel = switch_core_session_get_channel(peer_session);</div><div><span class="" style="white-space:pre">                                </span>char *a_key = (char *) switch_channel_get_variable(channel, &quot;bridge_terminate_key&quot;);</div><div><span class="" style="white-space:pre">                                </span>char *b_key = (char *) switch_channel_get_variable(peer_channel, &quot;bridge_terminate_key&quot;);</div><div><span class="" style="white-space:pre">                                </span>int ok = 0;</div><div><span class="" style="white-space:pre">                                </span>//switch_input_callback_function_t func = NULL;</div><div><span class="" style="white-space:pre">                                </span>rt-&gt;peer_channel = peer_channel;</div><div><span class="" style="white-space:pre">                                </span>rt-&gt;peer_profile = switch_channel_get_caller_profile(peer_channel);</div><div><br></div><div><span class="" style="white-space:pre">                                </span>if (a_key) {</div><div><span class="" style="white-space:pre">                                        </span>a_key = switch_core_session_strdup(session, a_key);</div><div><span class="" style="white-space:pre">                                        </span>ok++;</div><div><span class="" style="white-space:pre">                                </span>}</div><div><span class="" style="white-space:pre">                                </span>if (b_key) {</div><div><span class="" style="white-space:pre">                                        </span>b_key = switch_core_session_strdup(session, b_key);</div><div><span class="" style="white-space:pre">                                        </span>ok++;</div><div><span class="" style="white-space:pre">                                </span>}</div><div><span class="" style="white-space:pre">                                </span>if (switch_true(switch_channel_get_variable(caller_channel, SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE)) ||</div><div><span class="" style="white-space:pre">                                        </span>switch_true(switch_channel_get_variable(peer_channel, SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE))) {</div><div><span class="" style="white-space:pre">                                        </span>switch_channel_set_flag(caller_channel, CF_BYPASS_MEDIA_AFTER_BRIDGE);</div><div><span class="" style="white-space:pre">                                </span>}</div><div><br></div><div><span class="" style="white-space:pre">                                </span>switch_core_session_enable_heartbeat(session, rt-&gt;heartbeat_interval);<span class="" style="white-space:pre">        </span></div><div><br></div><div><span class="" style="white-space:pre">                                </span>// Bridge</div><div><span class="" style="white-space:pre">                                </span>switch_ivr_multi_threaded_bridge(session, peer_session, NULL, NULL, NULL);</div><div><span class="" style="white-space:pre">                                </span>if(peer_channel){</div><div><span class="" style="white-space:pre">                                        </span>if((peer_profile = switch_channel_get_caller_profile(peer_channel))){</div><div><span class="" style="white-space:pre">                                        </span>}</div><div><span class="" style="white-space:pre">                                        </span>rt-&gt;codec_out = switch_core_strdup(pool, switch_channel_get_variable(peer_channel, &quot;write_codec&quot;));</div><div><span class="" style="white-space:pre">                                </span>}</div><div><br></div><div><span class="" style="white-space:pre">                                </span>switch_core_session_disable_heartbeat(session);<span class="" style="white-space:pre">        </span></div><div><span class="" style="white-space:pre">                        </span>}</div><div><span class="" style="white-space:pre">                </span>}</div><div>------------------------------</div><div><br></div><div>At the end of the application function y use this code to do some tasks if the call gets answered:</div><div><br></div><div>------------------------------</div><div>...</div><div>  </div><div><span class="" style="white-space:pre">        </span>if(dc-&gt;rt-&gt;peer_channel &amp;&amp; switch_channel_test_flag(dc-&gt;rt-&gt;peer_channel, CF_ANSWERED)){</div><div>    // Tasks if answered</div><div>  }</div><div>...</div><div>------------------------------</div><div><br></div><div><br></div><div>I don&#39;t understand why sometimes switch_channel_test_flag generate a segfault. GDB stack trace is:</div><div><br></div><div><br></div><div>------------------------------</div><div>#0  0x00007f2947415420 in pthread_mutex_lock () from /lib64/libpthread.so.0</div><div>#1  0x00007f2948e522f3 in switch_channel_test_flag (channel=0x7f2934558a30,</div><div>    flag=CF_ANSWERED) at src/switch_channel.c:1539</div><div>#2  0x00007f292950dcf6 in procesar_hangup (session=0x7f293826b998)</div><div>    at /usr/src/instalacion/09freeswitch/freeswitch-1.4.23/src/mod/applications/mod_svbilling/mod_svbilling.c:4743</div><div>#3  0x00007f29295153a8 in do_routing (session=0x7f293826b998)</div><div>    at /usr/src/instalacion/09freeswitch/freeswitch-1.4.23/src/mod/applications/mod_svbilling/mod_svbilling.c:3937</div><div>#4  0x00007f2948e7d4d0 in switch_core_session_exec (session=0x7f293826b998,</div><div>    application_interface=0xd73208, arg=0x7f293429d550 &quot;process&quot;)</div><div>    at src/switch_core_session.c:2754</div><div>#5  0x00007f2948e7da32 in switch_core_session_execute_application_get_flags (</div><div>    session=0x7f293826b998, app=0x7f293429d540 &quot;svbillingapp&quot;,</div><div>    arg=0x7f293429d550 &quot;process&quot;, flags=&lt;value optimized out&gt;)</div><div>    at src/switch_core_session.c:2624</div><div>#6  0x00007f2948e81794 in switch_core_standard_on_execute (</div><div>    session=0x7f293826b998) at src/switch_core_state_machine.c:283</div><div>#7  switch_core_session_run (session=0x7f293826b998)</div><div>    at src/switch_core_state_machine.c:535</div><div>#8  0x00007f2948e7c23e in switch_core_session_thread (</div><div>    thread=&lt;value optimized out&gt;, obj=0x7f293826b998)</div><div>    at src/switch_core_session.c:1607</div><div>...</div><div>------------------------------</div><div><br></div><div>The generated core can be downloaded at:</div><div><br></div><div><a href="https://dl.dropboxusercontent.com/u/45510863/coretx/core.zip">https://dl.dropboxusercontent.com/u/45510863/coretx/core.zip</a></div><div><br></div><div>and the line generating the segfault inside switch_channel.c is</div><div><br></div><div>------------------------------      </div><div>switch_mutex_lock(channel-&gt;flag_mutex);</div><div>------------------------------</div><div><br></div><div>I suppose I am doing something in a wrong way but I don&#39;t know which one. Do anyone knows why?</div><div><br></div><div>Regards</div></div>