From 73cfad320d409f62e606f4f33bbed542f1123e06 Mon Sep 17 00:00:00 2001 From: Simon Morlat Date: Fri, 16 Oct 2015 22:59:28 +0200 Subject: [PATCH] fix memory leaks around presence --- coreapi/bellesip_sal/sal_op_presence.c | 2 +- coreapi/friend.c | 20 ++++++++++++++++++-- coreapi/linphonecore.c | 3 --- coreapi/presence.c | 16 +++++++++------- tester/presence_tester.c | 11 ++++++++++- 5 files changed, 38 insertions(+), 14 deletions(-) diff --git a/coreapi/bellesip_sal/sal_op_presence.c b/coreapi/bellesip_sal/sal_op_presence.c index f1326d3ec..c33416209 100644 --- a/coreapi/bellesip_sal/sal_op_presence.c +++ b/coreapi/bellesip_sal/sal_op_presence.c @@ -47,7 +47,7 @@ void sal_add_presence_info(SalOp *op, belle_sip_message_t *notify, SalPresenceMo } static void presence_process_io_error(void *user_ctx, const belle_sip_io_error_event_t *event){ - ms_error("presence_process_io_error not implemented yet"); + /*ms_error("presence_process_io_error not implemented yet");*/ } static void presence_process_dialog_terminated(void *ctx, const belle_sip_dialog_terminated_event_t *event) { diff --git a/coreapi/friend.c b/coreapi/friend.c index 250fdeed5..a9f7f4577 100644 --- a/coreapi/friend.c +++ b/coreapi/friend.c @@ -290,6 +290,17 @@ static void _linphone_friend_destroy(LinphoneFriend *lf){ if (lf->info!=NULL) buddy_info_free(lf->info); } +static belle_sip_error_code _linphone_friend_marshall(belle_sip_object_t *obj, char* buff, size_t buff_size, size_t *offset) { + LinphoneFriend *lf = (LinphoneFriend*)obj; + belle_sip_error_code err = BELLE_SIP_OK; + if (lf->uri){ + char *tmp = linphone_address_as_string(lf->uri); + err = belle_sip_snprintf(buff, buff_size, offset, "%s", tmp); + ms_free(tmp); + } + return err; +} + const LinphoneAddress *linphone_friend_get_address(const LinphoneFriend *lf){ return lf->uri; } @@ -491,6 +502,11 @@ void linphone_core_add_friend(LinphoneCore *lc, LinphoneFriend *lf) return ; } lc->friends=ms_list_append(lc->friends,linphone_friend_ref(lf)); + if (ms_list_find(lc->subscribers, lf)){ + /*if this friend was in the pending subscriber list, now remove it from this list*/ + lc->subscribers = ms_list_remove(lc->subscribers, lf); + linphone_friend_unref(lf); + } lf->lc=lc; if ( linphone_core_ready(lc)) linphone_friend_apply(lf,lc); else lf->commit=TRUE; @@ -500,7 +516,7 @@ void linphone_core_add_friend(LinphoneCore *lc, LinphoneFriend *lf) void linphone_core_remove_friend(LinphoneCore *lc, LinphoneFriend* fl){ MSList *el=ms_list_find(lc->friends,fl); if (el!=NULL){ - linphone_friend_destroy((LinphoneFriend*)el->data); + linphone_friend_unref((LinphoneFriend*)el->data); lc->friends=ms_list_remove_link(lc->friends,el); linphone_core_write_friends_config(lc); }else{ @@ -713,6 +729,6 @@ BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphoneFriend); BELLE_SIP_INSTANCIATE_VPTR(LinphoneFriend, belle_sip_object_t, (belle_sip_object_destroy_t) _linphone_friend_destroy, NULL, // clone - NULL, // marshal + _linphone_friend_marshall, FALSE ); \ No newline at end of file diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index 9543267e8..b52a2a632 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -6427,9 +6427,6 @@ static void linphone_core_uninit(LinphoneCore *lc) if (lc->chat_db_file){ ms_free(lc->chat_db_file); } - if(lc->presence_model){ - linphone_presence_model_unref(lc->presence_model); - } linphone_core_free_payload_types(lc); if (lc->supported_formats) ms_free(lc->supported_formats); linphone_core_message_storage_close(lc); diff --git a/coreapi/presence.c b/coreapi/presence.c index 22164a746..b10f1e35f 100644 --- a/coreapi/presence.c +++ b/coreapi/presence.c @@ -1444,17 +1444,19 @@ static LinphonePresenceModel * process_pidf_xml_presence_notification(xmlparsing void linphone_core_add_subscriber(LinphoneCore *lc, const char *subscriber, SalOp *op){ LinphoneFriend *fl=linphone_friend_new_with_address(subscriber); + char *tmp; + if (fl==NULL) return ; linphone_friend_add_incoming_subscription(fl, op); linphone_friend_set_inc_subscribe_policy(fl,LinphoneSPAccept); fl->inc_subscribe_pending=TRUE; - lc->subscribers=ms_list_append(lc->subscribers,(void *)linphone_friend_ref(fl)); - { - char *tmp=linphone_address_as_string(fl->uri); - linphone_core_notify_new_subscription_requested(lc,fl,tmp); - linphone_friend_unref(fl); - ms_free(tmp); - } + /* the newly created "not yet" friend ownership is transfered to the lc->subscribers list*/ + lc->subscribers=ms_list_append(lc->subscribers,fl); + + tmp = linphone_address_as_string(fl->uri); + linphone_core_notify_new_subscription_requested(lc,fl,tmp); + ms_free(tmp); + } void linphone_core_reject_subscriber(LinphoneCore *lc, LinphoneFriend *lf){ diff --git a/tester/presence_tester.c b/tester/presence_tester.c index 37ebd2faf..d106f1534 100644 --- a/tester/presence_tester.c +++ b/tester/presence_tester.c @@ -28,12 +28,14 @@ static LinphoneCoreManager* presence_linphone_core_manager_new(char* username) { linphone_address_set_username(mgr->identity,username); identity_char=linphone_address_as_string(mgr->identity); linphone_core_set_primary_contact(mgr->lc,identity_char); + ms_free(identity_char); return mgr; } + void new_subscription_requested(LinphoneCore *lc, LinphoneFriend *lf, const char *url){ char* from=linphone_address_as_string(linphone_friend_get_address(lf)); stats* counters; - ms_message("New subscription request from [%s] url [%s]",from,url); + ms_message("New subscription request from [%s] url [%s]",from,url); ms_free(from); counters = get_stats(lc); counters->number_of_NewSubscriptionRequest++; @@ -374,6 +376,13 @@ static void subscribe_presence_forked(){ /*we should get two notifies*/ BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphonePresenceActivityOnline,2, 10000)); + /*marie also shall receive two SUBSCRIBEs from the two paulines, but won't be notified to the app since + Marie set Pauline as a friend.*/ + BC_ASSERT_EQUAL(marie->stat.number_of_NewSubscriptionRequest, 0, int, "%d"); + /*and the two paulines shall be notified of marie's presence*/ + BC_ASSERT_TRUE(wait_for_list(lcs,&pauline1->stat.number_of_LinphonePresenceActivityOnline,1, 3000)); + BC_ASSERT_TRUE(wait_for_list(lcs,&pauline2->stat.number_of_LinphonePresenceActivityOnline,1, 2000)); + linphone_core_manager_destroy(marie); linphone_core_manager_destroy(pauline1); linphone_core_manager_destroy(pauline2);