From 62d1562669676641613f31f9949008b73d56b458 Mon Sep 17 00:00:00 2001 From: Tai Vuong Date: Tue, 7 Nov 2017 15:40:40 -0500 Subject: Fix memory leak, ducking bug on policy, close stream --- ahl-binding/ahl-binding.c | 21 ++++--------------- ahl-binding/ahl-binding.h | 2 +- ahl-binding/ahl-config.c | 4 ++-- ahl-binding/ahl-deviceenum.c | 50 ++++++++++++++++++++++++-------------------- 4 files changed, 34 insertions(+), 43 deletions(-) (limited to 'ahl-binding') diff --git a/ahl-binding/ahl-binding.c b/ahl-binding/ahl-binding.c index 0b2348a..83b4dc7 100644 --- a/ahl-binding/ahl-binding.c +++ b/ahl-binding/ahl-binding.c @@ -301,24 +301,12 @@ static void AhlBindingTerm() } } // Source endpoints - if (pRole->pSourceEndpoints) { - for (int i = 0; i < pRole->pSourceEndpoints->len; i++) - { - EndpointInfoT * pEndpoint = g_ptr_array_remove_index( pRole->pSourceEndpoints, i ); // Free endpoint * is called by GLib - if (pEndpoint) { - TermEndpointInfo(pEndpoint); - } - } + if (pRole->pSourceEndpoints) { + g_ptr_array_unref(pRole->pSourceEndpoints); } // Sink endpoints if (pRole->pSinkEndpoints) { - for (int i = 0; i < pRole->pSinkEndpoints->len; i++) - { - EndpointInfoT * pEndpoint = g_ptr_array_remove_index( pRole->pSinkEndpoints, i ); // Free endpoint * is called by GLib - if (pEndpoint) { - TermEndpointInfo(pEndpoint); - } - } + g_ptr_array_unref(pRole->pSinkEndpoints); } free(pRole); } @@ -455,8 +443,7 @@ PUBLIC int AhlBindingInit() { AFB_ERROR("Unable to create Active Stream List"); return AHL_FAIL; - } - + } AFB_DEBUG("Audio high-level Binding success"); return AHL_SUCCESS; } diff --git a/ahl-binding/ahl-binding.h b/ahl-binding/ahl-binding.h index 161c07b..6ceb040 100644 --- a/ahl-binding/ahl-binding.h +++ b/ahl-binding/ahl-binding.h @@ -108,7 +108,7 @@ PUBLIC void AhlOnEvent(const char *evtname, json_object *eventJ); // ahl-deviceenum.c int EnumerateDevices(json_object * in_jDeviceArray, char * in_pAudioRole, EndpointTypeT in_deviceType, GPtrArray * out_pEndpointArray); EndpointInfoT * InitEndpointInfo(); -void TermEndpointInfo( EndpointInfoT * out_pEndpointInfo ); +void TermEndpointInfo( gpointer data ); // ahl-config.c int ParseHLBConfig(); // ahl-policy.c diff --git a/ahl-binding/ahl-config.c b/ahl-binding/ahl-config.c index 0817c25..61b6edd 100644 --- a/ahl-binding/ahl-config.c +++ b/ahl-binding/ahl-config.c @@ -192,7 +192,7 @@ int ParseHLBConfig() { } // Sources - pRoleInfo->pSourceEndpoints = g_ptr_array_new_with_free_func(g_free); + pRoleInfo->pSourceEndpoints = g_ptr_array_new_with_free_func(TermEndpointInfo); if (iNumInDevices) { err = EnumerateDevices(jInputDevices,pRoleName,ENDPOINTTYPE_SOURCE,pRoleInfo->pSourceEndpoints); if (err) { @@ -201,7 +201,7 @@ int ParseHLBConfig() { } } // Sinks - pRoleInfo->pSinkEndpoints = g_ptr_array_new_with_free_func(g_free); + pRoleInfo->pSinkEndpoints = g_ptr_array_new_with_free_func(TermEndpointInfo); if (iNumOutDevices) { err = EnumerateDevices(jOutputDevices,pRoleName,ENDPOINTTYPE_SINK,pRoleInfo->pSinkEndpoints); if (err) { diff --git a/ahl-binding/ahl-deviceenum.c b/ahl-binding/ahl-deviceenum.c index f672417..b5b2a46 100644 --- a/ahl-binding/ahl-deviceenum.c +++ b/ahl-binding/ahl-deviceenum.c @@ -203,31 +203,35 @@ EndpointInfoT * InitEndpointInfo() return pEndpointInfo; } -void TermEndpointInfo( EndpointInfoT * out_pEndpointInfo ) +void TermEndpointInfo( gpointer data ) { + EndpointInfoT * out_pEndpointInfo = (EndpointInfoT *)data; #define SAFE_FREE(__ptr__) if(__ptr__) g_free(__ptr__); __ptr__ = NULL; - SAFE_FREE(out_pEndpointInfo->gsDeviceName); - SAFE_FREE(out_pEndpointInfo->gsDeviceDomain); - SAFE_FREE(out_pEndpointInfo->pRoleName); - SAFE_FREE(out_pEndpointInfo->gsDeviceURI); - SAFE_FREE(out_pEndpointInfo->gsHALAPIName); - SAFE_FREE(out_pEndpointInfo->gsDisplayName); - - if (out_pEndpointInfo->pPropTable) { - // Free json_object for all property values - GHashTableIter iter; - gpointer key, value; - g_hash_table_iter_init (&iter, out_pEndpointInfo->pPropTable); - while (g_hash_table_iter_next (&iter, &key, &value)) - { - if (value) - json_object_put(value); - } - g_hash_table_remove_all(out_pEndpointInfo->pPropTable); - g_hash_table_destroy(out_pEndpointInfo->pPropTable); - out_pEndpointInfo->pPropTable = NULL; - } - // GLib automatically frees item when removed from the array + if(out_pEndpointInfo) + { + SAFE_FREE(out_pEndpointInfo->gsDeviceName); + SAFE_FREE(out_pEndpointInfo->gsDeviceDomain); + SAFE_FREE(out_pEndpointInfo->pRoleName); + SAFE_FREE(out_pEndpointInfo->gsDeviceURI); + SAFE_FREE(out_pEndpointInfo->gsHALAPIName); + SAFE_FREE(out_pEndpointInfo->gsDisplayName); + + if (out_pEndpointInfo->pPropTable) { + // Free json_object for all property values + GHashTableIter iter; + gpointer key, value; + g_hash_table_iter_init (&iter, out_pEndpointInfo->pPropTable); + while (g_hash_table_iter_next (&iter, &key, &value)) + { + if (value) + json_object_put(value); + } + g_hash_table_remove_all(out_pEndpointInfo->pPropTable); + g_hash_table_destroy(out_pEndpointInfo->pPropTable); + out_pEndpointInfo->pPropTable = NULL; + } + g_slice_free (EndpointInfoT, out_pEndpointInfo); + } } // For a given audio role -- cgit 1.2.3-korg