Release resources for the notify file destriptor properly 59/216659/3 accepted/tizen/unified/20191101.042113 submit/tizen/20191031.005137
authorDoHyun Pyun <dh79.pyun@samsung.com>
Wed, 30 Oct 2019 06:43:35 +0000 (15:43 +0900)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Wed, 30 Oct 2019 07:10:05 +0000 (16:10 +0900)
If we close the fd only, io channel and watcher resource is
not released. So it occurs critical problems with next logs.

ecore<4672> ../src/lib/ecore/ecore_main.c:2083 _ecore_main_fd_handlers_bads_rem() Removing bad fds
ecore<4672> ../src/lib/ecore/ecore_main.c:2130 _ecore_main_fd_handlers_bads_rem() No bad fd found. Maybe a foreign fd from glib?

Change-Id: I683aa747018ff6139d31b48207b568da4dab8cae
Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
bt-api/bt-gatt-client.c
bt-api/include/bt-gatt-client.h

index 1b4cada..d400804 100755 (executable)
@@ -2262,11 +2262,15 @@ static bt_gatt_characteristic_notify_info_t *  bluetooth_gatt_client_create_watc
        memcpy(chr_info->UUID, uuid, 16);
 
        channel = g_io_channel_unix_new(fd);
+
+       chr_info->io_channel = channel;
+
        g_io_channel_set_encoding(channel, NULL, NULL);
        g_io_channel_set_buffered(channel, FALSE);
-       g_io_channel_set_close_on_unref(channel, FALSE);
+       g_io_channel_set_close_on_unref(channel, TRUE);
        g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
-       g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP),
+
+       chr_info->watch_id = g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP),
                         bluetooth_gatt_client_notify_channel_watch_cb, chr_info);
 
        return chr_info;
@@ -2296,7 +2300,18 @@ BT_EXPORT_API int bluetooth_gatt_client_watch_characteristics(
        if (chr_info && !is_notify) {
                BT_INFO("Already CCCD enabled. fd %d", chr_info->notify_fd);
 
-               close(chr_info->notify_fd);
+               if (chr_info->watch_id > 0)
+                       g_source_remove(chr_info->watch_id);
+
+               if (chr_info->io_channel) {
+                       g_io_channel_shutdown(chr_info->io_channel, TRUE, NULL);
+                       g_io_channel_unref(chr_info->io_channel);
+               }
+
+               gatt_characteristic_notify_list = g_slist_remove(gatt_characteristic_notify_list, chr_info);
+
+               g_free(chr_info);
+
                return result;
        }
 #endif
index 4ecc07c..8749ee9 100644 (file)
@@ -55,6 +55,8 @@ typedef struct {
        int mtu;
        unsigned char UUID[16];
        char address[BLUETOOTH_ADDRESS_STRING_LENGTH];
+       void *io_channel;
+       guint watch_id;
 } bt_gatt_characteristic_notify_info_t;