Fix resource leak : 23614
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / zblib_service.c
index ff46159..2bfb853 100644 (file)
@@ -256,7 +256,7 @@ static void __zblib_service_remove_request_table_iter(gpointer key,
        gpointer value, gpointer user_data)
 {
        ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
-       gint request_id = (gint)key;
+       gint request_id = GPOINTER_TO_INT(key);
 
        NOT_USED(value);
 
@@ -266,6 +266,22 @@ static void __zblib_service_remove_request_table_iter(gpointer key,
        zblib_request_free(service_interface, request_id);
 }
 
+static void __zblib_service_remove_async_list(GSList *list_async)
+{
+       GSList *iter = NULL;
+       if (0 < g_slist_length(list_async)) {
+               iter = g_slist_nth(list_async, 0);
+               while (iter) {
+                       if (iter) {
+                               struct _zblib_async_init_info *async_plugin =
+                                       (struct _zblib_async_init_info *)(list_async->data);
+                               list_async = g_slist_remove(list_async, async_plugin);
+                       }
+                       iter = g_slist_nth(list_async, 0);
+               }
+       }
+}
+
 ZigBeeService *zblib_service_new()
 {
        ZigBeeService *service;
@@ -424,6 +440,7 @@ gboolean zblib_service_load_plugins(ZigBeeService *service, const char *plugin_p
                /* Create new plug-in */
                plugin = zblib_plugin_new(service, filename, descriptor, handle);
                if (G_UNLIKELY(NULL == plugin)) {
+                       dlclose(handle);
                        g_free(filename);
                        continue;
                }
@@ -457,7 +474,7 @@ gboolean zblib_service_initialize_plugins(ZigBeeService *service)
                        /* If there is no initializer, it should have asynchronous one */
                        const ZblibPluginDescriptor_t *descriptor =
                                        zblib_plugin_get_descriptor(plugin);
-                       if (NULL != descriptor->init_async) {
+                       if (NULL != descriptor && NULL != descriptor->init_async) {
                                /* Register async initializer */
                                struct _zblib_async_init_info *info =
                                        g_try_new0(struct _zblib_async_init_info, 1);
@@ -472,6 +489,7 @@ gboolean zblib_service_initialize_plugins(ZigBeeService *service)
 
                                list_async = g_slist_append(list_async, info);
                        } else {
+                               __zblib_service_remove_async_list(list_async);
                                Z_LOGE("Fatal : Failed to initialize plugin");
                                return FALSE;
                        }
@@ -516,10 +534,15 @@ gboolean zblib_service_initialize_async_plugins(ZigBeeService *service)
 
 gboolean zblib_service_unload_plugins(ZigBeeService *service)
 {
-       GSList *list;
+       GSList *list = NULL;
+       GSList *list_async = NULL;
 
        zblib_check_null_ret_error("service", service, FALSE);
 
+       /* Unload everyy async_plugin */
+       list_async = __zblib_service_ref_async_initializer(service);
+       __zblib_service_remove_async_list(list_async);
+
        list = zblib_service_ref_plugins(service);
        while (list != NULL) {
                ZigBeePlugin *plugin = list->data;