Hello,
I'm trying to use Sofia-SIP stack and have written a very small server-like application which simply answers for invites, answers OK back with some SDP and finally hangs up the call when BYE is received. Everything works fine but there's a serious memory leak which I have not been able to track down...

Can someone please give some feedback on it and let me know if I am doing something wrong here? Should I free memory somewhere?
Thank you for your support!

Greetings,
Tiago

############################################################################################################################################################
typedef struct ssip_s      ssip_t;
typedef struct ssip_oper_s ssip_oper_t;

#define SU_ROOT_MAGIC_T     ssip_t
#define NUA_MAGIC_T         ssip_t
#define NUA_HMAGIC_T        ssip_oper_t

struct ssip_s {
  su_home_t       s_home[1];            /**< memory allocation object */
  su_root_t      *s_root;               /**< internal event loop */
  nua_t          *s_nua;                /**< SIP engine object */
  nua_handle_t   *s_nh;                 /**< This for presence */
};

/* Application context structure */
ssip_t appl[1] = {{{{(sizeof (ssip_t))}}}};

void app_i_invite(int           status,
                  char const   *phrase,
                  nua_t        *nua,
                  ssip_t  *ssip,
                  nua_handle_t *nh,
                  ssip_oper_t *op,
                  sip_t const  *sip,
                  tagi_t        tags[])
{
  printf("incoming call\n");

  nua_respond(nh, 200, "OK",
              SIPTAG_CONTACT_STR("sip:192.168.0.113:5060"),
              SOATAG_USER_SDP_STR("v=0\r\nm=audio 9440 RTP/AVP 18 101\r\na=alt:1 1 : F4E595DC 0000004F 192.168.0.113 9440\r\na=fmtp:101 0-15\r\na=rtpmap:101 telephone-event/8000\r\na=sendrecv\r\n"),
              SOATAG_RTP_SORT(SOA_RTP_SORT_REMOTE),
              SOATAG_RTP_SELECT(SOA_RTP_SELECT_ALL),
              TAG_END());
}

void app_i_state(int           status,
                  char const   *phrase,
                  nua_t        *nua,
                  nua_magic_t  *magic,
                  nua_handle_t *nh,
                  nua_hmagic_t *hmagic,
                  sip_t const  *sip,
                  tagi_t        tags[])
{
    printf("Status has changed: %03d %s\n", status, phrase);
}

void app_i_active(int           status,
                  char const   *phrase,
                  nua_t        *nua,
                  ssip_t  *ssip,
                  nua_handle_t *nh,
                  ssip_oper_t *op,
                  sip_t const  *sip,
                  tagi_t        tags[])
{
    printf("Call is now active: %03d %s\n", status, phrase);
}

void app_i_terminated(int           status,
                  char const   *phrase,
                  nua_t        *nua,
                  ssip_t  *ssip,
                  nua_handle_t *nh,
                  ssip_oper_t *op,
                  sip_t const  *sip,
                  tagi_t        tags[])
{
    nua_handle_destroy(nh); //Have tried with or without destroying this handle
}

void app_i_bye(int           status,
               char const   *phrase,
               nua_t        *nua,
               ssip_t  *ssip,
               nua_handle_t *nh,
               ssip_oper_t *op,
               sip_t const  *sip,
               tagi_t        tags[])
{
  printf("call released\n");
} /* app_i_bye */

void app_i_ack(int           status,
               char const   *phrase,
               nua_t        *nua,
               ssip_t  *ssip,
               nua_handle_t *nh,
               ssip_oper_t *op,
               sip_t const  *sip,
               tagi_t        tags[])
{
  printf("received ack\n");
} /* app_i_bye */

void app_callback(nua_event_t   event,
                  int           status,
                  char const   *phrase,
                  nua_t        *nua,
                  ssip_t  *magic,
                  nua_handle_t *nh,
                  ssip_oper_t *hmagic,
                  sip_t const  *sip,
                  tagi_t        tags[])
{
    switch (event) {
   case nua_i_invite:
        app_i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
        break;
       
    case nua_i_state:
        app_i_state(status, phrase, nua, magic, nh, hmagic, sip, tags);
        break;

    case nua_i_bye:
        app_i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
        break;

    case nua_i_active:
        app_i_active(status, phrase, nua, magic, nh, hmagic, sip, tags);
        break;

    case nua_i_terminated:
        app_i_terminated(status, phrase, nua, magic, nh, hmagic, sip, tags);
        break;

    case nua_i_ack:
        app_i_ack(status, phrase, nua, magic, nh, hmagic, sip, tags);
        break;

    default:
        /* unknown event -> print out error message */
        if (status > 100) {
            printf("unknown event %d: %03d %s\n",
                   event,
                   status,
                   phrase);
        }
        else {
            printf("unknown event %d\n", event);
        }
        tl_print(stdout, "", tags);
        break;
    }
} /* app_callback */


void stack_init()
{
    su_init();
    su_home_init(appl->s_home);
  
/* initialize root object */
    appl->s_root = su_root_create(appl);
   
    if (appl->s_root != NULL) {
        /* create NUA stack */
        appl->s_nua = nua_create(appl->s_root,
                                 app_callback,
                                 appl,
                                 /* tags as necessary ...*/
                                 TAG_NULL());
       
        if (appl->s_nua != NULL) {
            /* set necessary parameters */
            nua_set_params(appl->s_nua,
                           /* tags as necessary ... */
                           TAG_NULL());

            /* enter main loop for processing of messages */
            su_root_run(appl->s_root);
        }
    }
}

void stack_finish()
{
    if (appl->s_nua != NULL) {
        /* destroy NUA stack */
        nua_destroy(appl->s_nua);
    }
    /* deinit root object */
    su_root_destroy(appl->s_root);
    appl->s_root = NULL;

    /* deinitialize memory handling */
    su_home_deinit(appl->s_home);
/* deinitialize system utilities */
    su_deinit();
}

int main(int argc, char* argv[])
{
    stack_init();
    stack_finish();
    return 0;
}

_______________________________________________
Sofia-sip-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sofia-sip-devel

Reply via email to