From d9981b192ba7c101f276baa95fb8b655cc878514 Mon Sep 17 00:00:00 2001 From: Gilbok Lee Date: Wed, 15 Jun 2016 16:43:00 +0900 Subject: [PATCH] Fix memory leak issue Change-Id: Ic849df19b9d71c135522fe959b861b8c975d0841 Signed-off-by: Gilbok Lee --- include/port_gst/mediademuxer_port_gst.h | 3 +- src/mediademuxer.c | 14 ++++--- src/mediademuxer_port.c | 7 +++- src/port_gst/mediademuxer_port_gst.c | 67 ++++++++++++++++++++++---------- test/mediademuxer_test.c | 8 ++++ 5 files changed, 70 insertions(+), 29 deletions(-) diff --git a/include/port_gst/mediademuxer_port_gst.h b/include/port_gst/mediademuxer_port_gst.h index 8928813..8bda13a 100755 --- a/include/port_gst/mediademuxer_port_gst.h +++ b/include/port_gst/mediademuxer_port_gst.h @@ -87,8 +87,9 @@ typedef struct _mdgst_handle_t { GstElement *typefind; GstElement *demux; GstElement *fakesink; + GMainContext *thread_default; gulong signal_handoff; - gint bus_whatch_id; + guint bus_watch_id; bool is_valid_container; track_info info; int state_change_timeout; diff --git a/src/mediademuxer.c b/src/mediademuxer.c index e46e287..e2c67c1 100755 --- a/src/mediademuxer.c +++ b/src/mediademuxer.c @@ -123,8 +123,9 @@ int mediademuxer_prepare(mediademuxer_h demuxer) MD_E("[CoreAPI][%s] DEMUXER_ERROR_INVALID_OPERATION(0x%08x)", __FUNCTION__, MEDIADEMUXER_ERROR_INVALID_OPERATION); return MEDIADEMUXER_ERROR_INVALID_OPERATION; - } else + } else { handle->demux_state = MEDIADEMUXER_READY; + } } else { if (handle->demux_state != MEDIADEMUXER_IDLE) return MEDIADEMUXER_ERROR_INVALID_STATE; @@ -198,8 +199,9 @@ int mediademuxer_start(mediademuxer_h demuxer) MD_E("[CoreAPI][%s] DEMUXER_ERROR_INVALID_OPERATION(0x%08x)", __FUNCTION__, MEDIADEMUXER_ERROR_INVALID_OPERATION); return MEDIADEMUXER_ERROR_INVALID_OPERATION; - } else + } else { handle->demux_state = MEDIADEMUXER_DEMUXING; + } } else { if (handle->demux_state != MEDIADEMUXER_READY) return MEDIADEMUXER_ERROR_INVALID_STATE; @@ -380,21 +382,21 @@ int mediademuxer_destroy(mediademuxer_h demuxer) __FUNCTION__, MEDIADEMUXER_ERROR_INVALID_OPERATION); return MEDIADEMUXER_ERROR_INVALID_OPERATION; } else { - MD_E("[CoreAPI][%s] destroy handle : %p", __FUNCTION__, - handle); + MD_E("[CoreAPI][%s] destroy handle : %p", __FUNCTION__, handle); + handle->demux_state = MEDIADEMUXER_NONE; + g_free(handle); + handle = NULL; } } else { MD_E("[CoreAPI][%s] DEMUXER_ERROR_INVALID_OPERATION(0x%08x)", __FUNCTION__, MEDIADEMUXER_ERROR_INVALID_OPERATION); return MEDIADEMUXER_ERROR_INVALID_OPERATION; } - handle->demux_state = MEDIADEMUXER_NONE; return MEDIADEMUXER_ERROR_NONE; } int mediademuxer_get_state(mediademuxer_h demuxer, mediademuxer_state *state) { - MD_I("mediademuxer_get_state\n"); mediademuxer_error_e ret = MEDIADEMUXER_ERROR_NONE; DEMUXER_INSTANCE_CHECK(demuxer); mediademuxer_s *handle = (mediademuxer_s *)(demuxer); diff --git a/src/mediademuxer_port.c b/src/mediademuxer_port.c index cacc760..409d94b 100755 --- a/src/mediademuxer_port.c +++ b/src/mediademuxer_port.c @@ -259,17 +259,20 @@ int _md_util_parse(MMHandleType demuxer, const char *type) /*Set media_type depending upon the header of string else consider using file protocol */ if (new_demuxer->uri_src) { MD_L("new_demuxer->uri_src deallocating %p\n", new_demuxer->uri_src); - free(new_demuxer->uri_src); + g_free(new_demuxer->uri_src); + new_demuxer->uri_src = NULL; } new_demuxer->uri_src_media_type = __md_util_media_type(&media_type_string); if (new_demuxer->uri_src_media_type != MEDIADEMUXER_SRC_INVALID) { + if (new_demuxer->uri_src) + g_free(new_demuxer->uri_src); new_demuxer->uri_src = media_type_string; MD_L("uri:%s\n uri_type:%d\n", new_demuxer->uri_src, new_demuxer->uri_src_media_type); } else { MD_E("Error while setiing source\n"); MD_E("deallocating media_type_string %p\n", media_type_string); - free(media_type_string); + g_free(media_type_string); goto ERROR; } MEDIADEMUXER_FLEAVE(); diff --git a/src/port_gst/mediademuxer_port_gst.c b/src/port_gst/mediademuxer_port_gst.c index 514c81b..3a9e4c2 100755 --- a/src/port_gst/mediademuxer_port_gst.c +++ b/src/port_gst/mediademuxer_port_gst.c @@ -180,6 +180,10 @@ void __gst_free_stuct(track **head) MD_I("deallocate GST_PAD %p\n", temp->pad); gst_object_unref(temp->pad); } */ + if (temp->caps) { + MD_I("deallocate GST_PAD caps_ %p\n", temp->caps); + gst_caps_unref(temp->caps); + } if (temp->name) { MD_I("deallocate GST_PAD name %p\n", temp->name); g_free(temp->name); @@ -189,11 +193,10 @@ void __gst_free_stuct(track **head) temp->caps_string); g_free(temp->caps_string); } - if (temp->caps) { - MD_I("deallocate GST_PAD caps_ %p\n", temp->caps); - gst_caps_unref(temp->caps); + if (temp->format) { + MD_I("unref media_format %p\n", temp->format); + media_format_unref(temp->format); } - if (temp->next) { track *next = temp->next; MD_I("deallocate memory %p\n", temp); @@ -286,12 +289,17 @@ int __gst_add_track_info(GstPad *pad, gchar *name, track **head, prev = prev->next; prev->next = temp; } - gst_object_unref(apppad); + if (apppad) + gst_object_unref(apppad); + if (parse_sink_pad) + gst_object_unref(parse_sink_pad); MEDIADEMUXER_FLEAVE(); return MD_ERROR_NONE; ERROR: if (apppad) gst_object_unref(apppad); + if (parse_sink_pad) + gst_object_unref(parse_sink_pad); __gst_free_stuct(head); MEDIADEMUXER_FLEAVE(); return MD_ERROR; @@ -447,10 +455,31 @@ static int __gst_create_audio_only_pipeline(gpointer data, GstCaps *caps) /* link queue with aacparse */ MEDIADEMUXER_LINK_PAD(queue_srcpad, aud_pad, ERROR); } else { - MEDIADEMUXER_LINK_PAD(pad, aud_pad, ERROR); + MEDIADEMUXER_LINK_PAD(pad, aud_pad, ERROR); } if (adif_queue) MEDIADEMUXER_SET_STATE(adif_queue, GST_STATE_PAUSED, ERROR); + + trck = head_track->head; + while (trck != NULL && aud_srcpad != trck->pad) + trck = trck->next; + + if (trck != NULL) { + if (trck->caps) + gst_caps_unref(trck->caps); + trck->caps = caps; + if (trck->caps_string) + g_free(trck->caps_string); + trck->caps_string = gst_caps_to_string(trck->caps); + MD_I("caps set to %s\n", trck->caps_string); + if (trck->name) + g_free(trck->name); + g_strlcpy(name, "audio", strlen(name)); + trck->name = name; + } + (head_track->num_audio_track)++; + + /* unref pads */ if (pad) gst_object_unref(pad); if (aud_pad) @@ -464,17 +493,6 @@ static int __gst_create_audio_only_pipeline(gpointer data, GstCaps *caps) if (aud_srcpad) gst_object_unref(aud_srcpad); - trck = head_track->head; - while (trck != NULL && aud_srcpad != trck->pad) - trck = trck->next; - if (trck != NULL) { - trck->caps = caps; - trck->caps_string = gst_caps_to_string(trck->caps); - MD_I("caps set to %s\n", trck->caps_string); - g_strlcpy(name, "audio", strlen(name)); - trck->name = name; - } - (head_track->num_audio_track)++; __gst_no_more_pad(gst_handle->demux, data); g_free(type); MEDIADEMUXER_FLEAVE(); @@ -659,8 +677,9 @@ static int _gst_create_pipeline(mdgst_handle_t *gst_handle, char *uri) /* connect signals, bus watcher */ bus = gst_pipeline_get_bus(GST_PIPELINE(gst_handle->pipeline)); - gst_handle->bus_whatch_id = gst_bus_add_watch(bus, __gst_bus_call, gst_handle); - gst_object_unref(bus); + gst_handle->bus_watch_id = gst_bus_add_watch(bus, __gst_bus_call, gst_handle); + gst_handle->thread_default = g_main_context_get_thread_default(); + gst_object_unref(GST_OBJECT(bus)); /* set pipeline state to PAUSED */ MEDIADEMUXER_SET_STATE(gst_handle->pipeline, GST_STATE_PAUSED, ERROR); @@ -1025,7 +1044,6 @@ static int gst_demuxer_get_track_info(MMHandleType pHandle, ret = MD_INTERNAL_ERROR; goto ERROR; } - MEDIADEMUXER_FLEAVE(); return ret; ERROR: @@ -1376,6 +1394,15 @@ static int gst_demuxer_unprepare(MMHandleType pHandle) mdgst_handle_t *gst_handle = (mdgst_handle_t *) pHandle; _gst_clear_struct(gst_handle); + if (gst_handle->bus_watch_id) { + GSource *source = NULL; + source = g_main_context_find_source_by_id(gst_handle->thread_default, gst_handle->bus_watch_id); + if (source) { + g_source_destroy(source); + LOGD("Deallocation bus watch id"); + } + } + MD_I("gst_demuxer_stop pipeine %p\n", gst_handle->pipeline); if (_md_gst_destroy_pipeline(gst_handle->pipeline) != MD_ERROR_NONE) { ret = MD_ERROR; diff --git a/test/mediademuxer_test.c b/test/mediademuxer_test.c index ec7b89a..37ec468 100755 --- a/test/mediademuxer_test.c +++ b/test/mediademuxer_test.c @@ -390,6 +390,10 @@ void *_fetch_audio_data(void *ptr) { int ret = MEDIADEMUXER_ERROR_NONE; int *status = (int *)g_malloc(sizeof(int) * 1); + if (!status) { + g_print("Fail malloc fetch audio data retur status value\n"); + return NULL; + } media_packet_h audbuf; int count = 0; uint64_t buf_size = 0; @@ -583,6 +587,10 @@ void *_fetch_video_data(void *ptr) { int ret = MEDIADEMUXER_ERROR_NONE; int *status = (int *)g_malloc(sizeof(int) * 1); + if (!status) { + g_print("Fail malloc fetch video data retur status value\n"); + return NULL; + } media_packet_h vidbuf; int count = 0; uint64_t buf_size = 0; -- 2.7.4