From: Seungbae Shin Date: Tue, 31 Jan 2017 14:06:15 +0000 (+0900) Subject: Fix memory leak : valgrind detected X-Git-Tag: submit/tizen_3.0/20170201.113021^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F85%2F112385%2F3;p=platform%2Fcore%2Fapi%2Fsound-manager.git Fix memory leak : valgrind detected [Version] 0.3.94 [Profile] Common [Issue Type] Memory Leak Change-Id: I64d7d5e7cf44a65d5165de4b43afc2151352abfe --- diff --git a/include/sound_manager_private.h b/include/sound_manager_private.h index ea472b4..0c670c8 100644 --- a/include/sound_manager_private.h +++ b/include/sound_manager_private.h @@ -112,6 +112,13 @@ if (pthread_mutex_unlock(x_mutex)) { \ x_count--; \ } \ +#define SM_SAFE_FREE(x_val) do { \ + if (x_val) { \ + free(x_val); \ + x_val = NULL; \ + } \ +} while (0) + #define SOUND_SESSION_TYPE_DEFAULT SOUND_SESSION_TYPE_MEDIA #define SOUND_STREAM_INFO_ARR_MAX 128 #define SOUND_STREAM_TYPE_LEN 64 @@ -140,10 +147,10 @@ typedef enum stream_route_type { typedef struct _stream_conf_info_s { int priority; int route_type; - const gchar *volume_type; - gchar *avail_in_devices[AVAIL_DEVICES_MAX]; - gchar *avail_out_devices[AVAIL_DEVICES_MAX]; - gchar *avail_frameworks[AVAIL_FRAMEWORKS_MAX]; + char *volume_type; + char *avail_in_devices[AVAIL_DEVICES_MAX]; + char *avail_out_devices[AVAIL_DEVICES_MAX]; + char *avail_frameworks[AVAIL_FRAMEWORKS_MAX]; } stream_conf_info_s; typedef struct _manual_route_info_s { diff --git a/packaging/capi-media-sound-manager.spec b/packaging/capi-media-sound-manager.spec index 686aff4..5e54c06 100755 --- a/packaging/capi-media-sound-manager.spec +++ b/packaging/capi-media-sound-manager.spec @@ -1,6 +1,6 @@ Name: capi-media-sound-manager Summary: Sound Manager library -Version: 0.3.93 +Version: 0.3.94 Release: 0 Group: Multimedia/API License: Apache-2.0 diff --git a/src/sound_manager.c b/src/sound_manager.c index d50c826..c159e8e 100644 --- a/src/sound_manager.c +++ b/src/sound_manager.c @@ -241,8 +241,8 @@ int sound_manager_create_stream_information(sound_stream_type_e stream_type, sou } LEAVE: - if (ret && stream_h) - free(stream_h); + if (ret) + SM_SAFE_FREE(stream_h); SM_LEAVE_CRITICAL_SECTION(&g_stream_info_count_mutex); @@ -261,8 +261,7 @@ int sound_manager_destroy_stream_information(sound_stream_info_h stream_info) SM_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_stream_info_count_mutex, MM_ERROR_SOUND_INTERNAL); ret = _destroy_pa_connection_and_unregister_focus(stream_h); if (ret == MM_ERROR_NONE) { - free(stream_h); - stream_h = NULL; + SM_SAFE_FREE(stream_h); SM_UNREF_FOR_STREAM_INFO(g_stream_info_count); } SM_LEAVE_CRITICAL_SECTION(&g_stream_info_count_mutex); @@ -489,7 +488,7 @@ int sound_manager_get_current_playback_focus(sound_stream_focus_change_reason_e if (extra_info) *extra_info = extra_info_str; else - free(extra_info_str); + SM_SAFE_FREE(extra_info_str); } } @@ -527,7 +526,7 @@ int sound_manager_get_current_recording_focus(sound_stream_focus_change_reason_e if (extra_info) *extra_info = extra_info_str; else - free(extra_info_str); + SM_SAFE_FREE(extra_info_str); } } @@ -682,8 +681,7 @@ int sound_manager_set_session_type(sound_session_type_e type) /*voip stream destruction*/ if (g_voip_stream_info) { ret = _destroy_pa_connection_and_unregister_focus(g_voip_stream_info); - free(g_voip_stream_info); - g_voip_stream_info = NULL; + SM_SAFE_FREE(g_voip_stream_info); if (ret != MM_ERROR_NONE) return _convert_sound_manager_error_code(__func__, ret); } diff --git a/src/sound_manager_internal.c b/src/sound_manager_internal.c index ba352ef..33142f7 100644 --- a/src/sound_manager_internal.c +++ b/src/sound_manager_internal.c @@ -96,8 +96,8 @@ int sound_manager_create_stream_information_internal(sound_stream_type_internal_ } LEAVE: - if (ret && stream_h) - free(stream_h); + if (ret) + SM_SAFE_FREE(stream_h); SM_LEAVE_CRITICAL_SECTION(&g_stream_info_count_mutex); diff --git a/src/sound_manager_private.c b/src/sound_manager_private.c index 9e17d36..6d0374f 100644 --- a/src/sound_manager_private.c +++ b/src/sound_manager_private.c @@ -1329,8 +1329,7 @@ int _set_session_mode(_session_mode_e mode) g_voip_vstream_h = NULL; /* destroy stream info */ _destroy_pa_connection_and_unregister_focus(g_voip_stream_info); - free(g_voip_stream_info); - g_voip_stream_info = NULL; + SM_SAFE_FREE(g_voip_stream_info); } if (!g_voip_stream_info) { g_voip_stream_info = malloc(sizeof(sound_stream_info_s)); @@ -1344,8 +1343,7 @@ int _set_session_mode(_session_mode_e mode) g_voip_stream_info->stream_type = "ringtone-voip"; ret = _make_pa_connection_and_register_focus(g_voip_stream_info, true, _voip_focus_state_change_callback, NULL); if (ret != MM_ERROR_NONE) { - free(g_voip_stream_info); - g_voip_stream_info = NULL; + SM_SAFE_FREE(g_voip_stream_info); goto ERROR_CASE; } else { /* acquire focus */ @@ -1433,8 +1431,7 @@ int _set_session_mode(_session_mode_e mode) g_voip_vstream_h = NULL; /* destroy stream info */ _destroy_pa_connection_and_unregister_focus(g_voip_stream_info); - free(g_voip_stream_info); - g_voip_stream_info = NULL; + SM_SAFE_FREE(g_voip_stream_info); } /* create stream info and acquire focus for voip stream */ if (g_cached_session_mode == -1 || g_cached_session_mode == _SESSION_MODE_RINGTONE) { @@ -1449,8 +1446,7 @@ int _set_session_mode(_session_mode_e mode) g_voip_stream_info->stream_type = "voip"; ret = _make_pa_connection_and_register_focus(g_voip_stream_info, true, _voip_focus_state_change_callback, NULL); if (ret != MM_ERROR_NONE) { - free(g_voip_stream_info); - g_voip_stream_info = NULL; + SM_SAFE_FREE(g_voip_stream_info); goto ERROR_CASE; } /* set device for routing to the device */ @@ -1513,8 +1509,7 @@ ERROR_CASE: if (g_voip_stream_info) { /* destroy stream info */ _destroy_pa_connection_and_unregister_focus(g_voip_stream_info); - free(g_voip_stream_info); - g_voip_stream_info = NULL; + SM_SAFE_FREE(g_voip_stream_info); } ERROR_CASE_NO_DESTROY: return ret; @@ -1609,23 +1604,14 @@ PA_ERROR_WITH_UNLOCK: PA_ERROR: for (i = 0; i < AVAIL_DEVICES_MAX; i++) { - if (stream_h->stream_conf_info.avail_in_devices[i]) - free(stream_h->stream_conf_info.avail_in_devices[i]); - else - break; - } - for (i = 0; i < AVAIL_DEVICES_MAX; i++) { - if (stream_h->stream_conf_info.avail_out_devices[i]) - free(stream_h->stream_conf_info.avail_out_devices[i]); - else - break; - } - for (i = 0; i < AVAIL_FRAMEWORKS_MAX; i++) { - if (stream_h->stream_conf_info.avail_frameworks[i]) - free(stream_h->stream_conf_info.avail_frameworks[i]); - else - break; + SM_SAFE_FREE(stream_h->stream_conf_info.avail_in_devices[i]); + SM_SAFE_FREE(stream_h->stream_conf_info.avail_out_devices[i]); } + for (i = 0; i < AVAIL_FRAMEWORKS_MAX; i++) + SM_SAFE_FREE(stream_h->stream_conf_info.avail_frameworks[i]); + + SM_SAFE_FREE(stream_h->stream_conf_info.volume_type); + if (stream_h->pa_context) { pa_context_disconnect(stream_h->pa_context); pa_context_unref(stream_h->pa_context); @@ -1674,15 +1660,13 @@ int _destroy_pa_connection_and_unregister_focus(sound_stream_info_s *stream_h) } for (i = 0; i < AVAIL_DEVICES_MAX; i++) { - if (stream_h->stream_conf_info.avail_in_devices[i]) - free(stream_h->stream_conf_info.avail_in_devices[i]); - if (stream_h->stream_conf_info.avail_out_devices[i]) - free(stream_h->stream_conf_info.avail_out_devices[i]); - } - for (i = 0; i < AVAIL_FRAMEWORKS_MAX; i++) { - if (stream_h->stream_conf_info.avail_frameworks[i]) - free(stream_h->stream_conf_info.avail_frameworks[i]); + SM_SAFE_FREE(stream_h->stream_conf_info.avail_in_devices[i]); + SM_SAFE_FREE(stream_h->stream_conf_info.avail_out_devices[i]); } + for (i = 0; i < AVAIL_FRAMEWORKS_MAX; i++) + SM_SAFE_FREE(stream_h->stream_conf_info.avail_frameworks[i]); + + SM_SAFE_FREE(stream_h->stream_conf_info.volume_type); for (i = 0; i < SOUND_STREAM_INFO_ARR_MAX; i++) { if (sound_stream_info_arr[i] && sound_stream_info_arr[i]->index == stream_h->index) { @@ -1938,8 +1922,7 @@ int _destroy_virtual_stream(virtual_sound_stream_info_s *virtual_stream) if (virtual_stream->pa_proplist) pa_proplist_free(virtual_stream->pa_proplist); - free(virtual_stream); - virtual_stream = NULL; + SM_SAFE_FREE(virtual_stream); return ret; } diff --git a/test/sound_manager_test.c b/test/sound_manager_test.c index 9542724..95d1892 100644 --- a/test/sound_manager_test.c +++ b/test/sound_manager_test.c @@ -1241,7 +1241,7 @@ static void interpret(char *cmd) reset_menu_state(); break; } - if (!(ret = sound_manager_get_current_device_list(SOUND_DEVICE_ALL_MASK, &g_device_list))) { + if (!(ret = sound_manager_get_device_list(SOUND_DEVICE_ALL_MASK, &g_device_list))) { g_print("success to get current device list\n"); while (!sound_manager_get_next_device(g_device_list, &device)) { if (!(ret = sound_manager_get_device_type(device, &type))) { @@ -1255,6 +1255,7 @@ static void interpret(char *cmd) break; } } + sound_manager_free_device_list(g_device_list); if (need_to_go) { ret = sound_manager_add_device_for_stream_routing(g_stream_info_h, device); if (ret)