From 509b105bb64dcdbc99073639575a3e55670aa9ae Mon Sep 17 00:00:00 2001 From: Johan Pascal Date: Mon, 2 Jun 2014 06:58:19 +0200 Subject: [PATCH] File transfer: fix memory leak --- coreapi/chat.c | 12 +++++++++++- coreapi/help/filetransfer.c | 3 +++ coreapi/info.c | 4 +++- coreapi/private.h | 1 + 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/coreapi/chat.c b/coreapi/chat.c index 02638a64c..ecb8d395b 100644 --- a/coreapi/chat.c +++ b/coreapi/chat.c @@ -153,6 +153,8 @@ static void linphone_chat_message_process_response_from_post_file(void *data, co if (code == 200 ) { /* file has been uplaoded correctly, get server reply and send it */ const char *body = belle_sip_message_get_body((belle_sip_message_t *)event->response); msg->message = ms_strdup(body); + linphone_content_uninit(msg->file_transfer_information); + ms_free(msg->file_transfer_information); msg->file_transfer_information = NULL; msg->content_type = ms_strdup("application/vnd.gsma.rcs-ft-http+xml"); _linphone_chat_room_send_message(msg->chat_room, msg); @@ -644,6 +646,7 @@ LinphoneChatMessage* linphone_chat_room_create_message(LinphoneChatRoom *cr, con msg->message=message?ms_strdup(message):NULL; msg->is_read=TRUE; msg->content_type = NULL; /* this property is used only when transfering file */ + msg->file_transfer_information = NULL; /* this property is used only when transfering file */ return msg; } @@ -671,6 +674,7 @@ LinphoneChatMessage* linphone_chat_room_create_message_2( msg->state=state; msg->is_read=is_read; msg->content_type = NULL; /* this property is used only when transfering file */ + msg->file_transfer_information = NULL; /* this property is used only when transfering file */ if (is_incoming) { msg->dir=LinphoneChatMessageIncoming; linphone_chat_message_set_from(msg, linphone_chat_room_get_peer_address(cr)); @@ -1135,6 +1139,10 @@ static void _linphone_chat_message_destroy(LinphoneChatMessage* msg) { if (msg->to) linphone_address_destroy(msg->to); if (msg->custom_headers) sal_custom_header_free(msg->custom_headers); if (msg->content_type) ms_free(msg->content_type); + if (msg->file_transfer_information) { + linphone_content_uninit(msg->file_transfer_information); + ms_free(msg->file_transfer_information); + } } @@ -1182,7 +1190,9 @@ LinphoneChatMessage* linphone_chat_room_create_file_transfer_message(LinphoneCha LinphoneChatMessage* msg = belle_sip_object_new(LinphoneChatMessage); msg->chat_room=(LinphoneChatRoom*)cr; msg->message = NULL; - msg->file_transfer_information = initial_content; + msg->file_transfer_information = (LinphoneContent *)malloc(sizeof(LinphoneContent)); + memset(msg->file_transfer_information, 0, sizeof(LinphoneContent)); + linphone_content_copy(msg->file_transfer_information, initial_content); msg->dir=LinphoneChatMessageOutgoing; linphone_chat_message_set_to(msg, linphone_chat_room_get_peer_address(cr)); linphone_chat_message_set_from(msg, linphone_address_new(linphone_core_get_identity(cr->lc))); diff --git a/coreapi/help/filetransfer.c b/coreapi/help/filetransfer.c index b45019c43..c297edaf2 100644 --- a/coreapi/help/filetransfer.c +++ b/coreapi/help/filetransfer.c @@ -82,6 +82,8 @@ static void file_transfer_received(LinphoneCore *lc, LinphoneChatMessage *messag if (size==0) { printf("File transfert completed\n"); + linphone_chat_room_destroy(linphone_chat_message_get_chat_room(message)); + linphone_chat_message_destroy(message); close(file); running=FALSE; } else { /* store content on a file*/ @@ -220,6 +222,7 @@ int main(int argc, char *argv[]){ ms_usleep(50000); } + printf("Shutting down...\n"); linphone_chat_room_destroy(chat_room); linphone_core_destroy(lc); diff --git a/coreapi/info.c b/coreapi/info.c index ff7ff3c93..2bf2b85ad 100644 --- a/coreapi/info.c +++ b/coreapi/info.c @@ -42,10 +42,11 @@ struct _LinphoneInfoMessage{ ptr->field=ms_strdup(val); \ } -static void linphone_content_copy(LinphoneContent *obj, const LinphoneContent *ref){ +void linphone_content_copy(LinphoneContent *obj, const LinphoneContent *ref){ SET_STRING(obj,type,ref->type); SET_STRING(obj,subtype,ref->subtype); SET_STRING(obj,encoding,ref->encoding); + SET_STRING(obj,name,ref->name); if (obj->data) { ms_free(obj->data); obj->data=NULL; @@ -63,6 +64,7 @@ void linphone_content_uninit(LinphoneContent * obj){ if (obj->subtype) ms_free(obj->subtype); if (obj->data) ms_free(obj->data); if (obj->encoding) ms_free(obj->encoding); + if (obj->name) ms_free(obj->name); } LinphoneContent *linphone_content_copy_from_sal_body(LinphoneContent *obj, const SalBody *ref){ diff --git a/coreapi/private.h b/coreapi/private.h index bcf683c9e..581bd6bb9 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -818,6 +818,7 @@ void linphone_call_create_op(LinphoneCall *call); int linphone_call_prepare_ice(LinphoneCall *call, bool_t incoming_offer); void linphone_core_notify_info_message(LinphoneCore* lc,SalOp *op, const SalBody *body); void linphone_content_uninit(LinphoneContent * obj); +void linphone_content_copy(LinphoneContent *obj, const LinphoneContent *ref); LinphoneContent *linphone_content_copy_from_sal_body(LinphoneContent *obj, const SalBody *ref); SalBody *sal_body_from_content(SalBody *body, const LinphoneContent *lc); SalReason linphone_reason_to_sal(LinphoneReason reason);