title: Duplicate Custom Headers on PJSIP Module
body: <p>I'm using PJSUA2 to build a custom VoIP application, for this particular application, I need to add a custom header for every single SIP message (Request/Response). I built a simple pjsip module to intercept every tx message and add a custom header.</p>
  <p>Although the custom header appears only once on the first response (Trying), duplicate headers appear in consequent responses (Ringing &amp; OK). Also number of duplicate headers seems to be increasing in an incremental fashion, indicating the header objects that I create are somehow accumulating somewhere.</p>
  <p>It is also important to note that the problem doesn't exist in Request messages. I use the below code to add headers to my requests however only single header appears, as expected. I temporarily solved the problem by adding an IF statement to check if the header already exists and my application is working fine now. However I'd like to know why the header that I add for a particular message appearing in the consequent messages, and how can I do this the right way.</p>
  <pre><code>static pj_bool_t on_tx_response(pjsip_tx_data *tdata) {
    const pj_str_t hName = { &quot;Test_Header&quot;, 11}; 
    const pj_str_t hValue = { &quot;test_val&quot;, 8}; 
    pjsip_hdr *h; h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata-&gt;pool, &amp;hName, &amp;hValue); 
    pjsip_msg_add_hdr(tdata-&gt;msg, h); return PJ_SUCCESS; }
  
  
  
  
  
  
  static pj_bool_t on_tx_request(pjsip_tx_data *tdata){
  
  const pj_str_t hName = { &quot;Test_Header&quot;, 11};
  const pj_str_t hValue = { &quot;test_val&quot;, 8};
  pjsip_hdr *h;
  h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata-&gt;pool, &amp;hName, &amp;hValue);
  pjsip_msg_add_hdr(tdata-&gt;msg, h);
  
  return PJ_SUCCESS;}
  </code></pre>
  <pre><code>Via: SIP/2.0/UDP 10.0.0.45:5060;rport=5060;received=10.0.0.45;branch=z9hG4bKPj.naQvzB0C1-quuuY0FII8HrxPITGgPaH
  Call-ID: TBtuKEEXoWFDCIE.7PRB7rIo.jN8VU6s
  From: &lt;sip:test@10.0.0.45&gt;;tag=aKxAh285xWKHhMeZHk5Ra2OF0SQJhO.j
  To: &lt;sip:test@10.0.0.45&gt;
  CSeq: 7730 INVITE
  **Test_Header: test_val**
  Content-Length:  0
   
   
  SIP/2.0 180 Ringing
  Via: SIP/2.0/UDP 10.0.0.45:5060;rport=5060;received=10.0.0.45;branch=z9hG4bKPj.naQvzB0C1-quuuY0FII8HrxPITGgPaH
  Call-ID: TBtuKEEXoWFDCIE.7PRB7rIo.jN8VU6s
  From: &lt;sip:test@10.0.0.45&gt;;tag=aKxAh285xWKHhMeZHk5Ra2OF0SQJhO.j
  To: &lt;sip:test@10.0.0.45&gt;;tag=-G9.K9Vm92Vv-Us7fZYd8o0lutz8n-yY
  CSeq: 7730 INVITE
  **Test_Header: test_val**
  Contact: &lt;sip:test@10.0.0.45:5060&gt;
  Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS
  **Test_Header: test_val**
  Content-Length:  0
   
  
  SIP/2.0 200 OK
  Via: SIP/2.0/UDP 10.0.0.45:5060;rport=5060;received=10.0.0.45;branch=z9hG4bKPj.naQvzB0C1-quuuY0FII8HrxPITGgPaH
  Call-ID: TBtuKEEXoWFDCIE.7PRB7rIo.jN8VU6s
  From: &lt;sip:test@10.0.0.45&gt;;tag=aKxAh285xWKHhMeZHk5Ra2OF0SQJhO.j
  To: &lt;sip:test@10.0.0.45&gt;;tag=-G9.K9Vm92Vv-Us7fZYd8o0lutz8n-yY
  CSeq: 7730 INVITE
  **Test_Header: test_val**
  Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS
  **Test_Header: test_val**
  Contact: &lt;sip:test@10.0.0.45:5060&gt;
  Supported: replaces, 100rel, timer, norefersub
  Session-Expires: 1800;refresher=uac
  Require: timer
  **Test_Header: test_val**
  Content-Type: application/sdp
  Content-Length:   307
   
  </code></pre>