@@ -39,6 +39,7 @@ gboolean on_handle_device_property_change(
39
39
gpointer user_data )
40
40
{
41
41
gatt_connection_t * connection = user_data ;
42
+ if (connection == NULL ) return ;
42
43
gattlib_context_t * conn_context = connection -> context ;
43
44
44
45
// Retrieve 'Value' from 'arg_changed_properties'
@@ -52,14 +53,23 @@ gboolean on_handle_device_property_change(
52
53
if (strcmp (key , "Connected" ) == 0 ) {
53
54
if (!g_variant_get_boolean (value )) {
54
55
// Disconnection case
56
+ if (conn_context -> handler_id != 0 ) {
57
+ g_signal_handler_disconnect (conn_context -> device ,conn_context -> handler_id );
58
+ conn_context -> handler_id = 0 ;
59
+ }
55
60
if (gattlib_has_valid_handler (& connection -> disconnection )) {
56
61
gattlib_call_disconnection_handler (& connection -> disconnection );
57
62
}
58
63
}
59
64
} else if (strcmp (key , "ServicesResolved" ) == 0 ) {
60
65
if (g_variant_get_boolean (value )) {
66
+ if (conn_context -> handler_id == NULL ) {
67
+ return false;
68
+ }
61
69
// Stop the timeout for connection
62
- g_source_remove (conn_context -> connection_timeout );
70
+ if ((conn_context != NULL ) && (conn_context -> connection_loop != NULL ) && (connection != NULL )) {
71
+ g_source_remove (conn_context -> connection_timeout );
72
+ }
63
73
64
74
// Tell we are now connected
65
75
g_main_loop_quit (conn_context -> connection_loop );
@@ -175,7 +185,7 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
175
185
}
176
186
177
187
// Register a handle for notification
178
- g_signal_connect (device ,
188
+ conn_context -> handler_id = g_signal_connect (device ,
179
189
"g-properties-changed" ,
180
190
G_CALLBACK (on_handle_device_property_change ),
181
191
connection );
@@ -201,15 +211,21 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
201
211
// and 'org.bluez.GattCharacteristic1' to be advertised at that moment.
202
212
conn_context -> connection_loop = g_main_loop_new (NULL , 0 );
203
213
214
+ if (conn_context == NULL ) goto FREE_DEVICE ;
204
215
conn_context -> connection_timeout = g_timeout_add_seconds (CONNECT_TIMEOUT , stop_scan_func ,
205
216
conn_context -> connection_loop );
217
+ if (conn_context == NULL ) goto FREE_DEVICE ;
206
218
g_main_loop_run (conn_context -> connection_loop );
219
+ if (conn_context == NULL ) goto FREE_DEVICE ;
207
220
g_main_loop_unref (conn_context -> connection_loop );
208
221
// Set the attribute to NULL even if not required
209
222
conn_context -> connection_loop = NULL ;
210
223
211
224
// Get list of objects belonging to Device Manager
212
225
device_manager = get_device_manager_from_adapter (conn_context -> adapter );
226
+ if (device_manager == NULL || (conn_context == NULL ) || (conn_context -> adapter == NULL )) {
227
+ goto FREE_DEVICE ;
228
+ }
213
229
conn_context -> dbus_objects = g_dbus_object_manager_get_objects (device_manager );
214
230
215
231
return connection ;
@@ -254,7 +270,16 @@ int gattlib_disconnect(gatt_connection_t* connection) {
254
270
g_object_unref (conn_context -> device );
255
271
g_list_free_full (conn_context -> dbus_objects , g_object_unref );
256
272
disconnect_all_notifications (conn_context );
273
+ if (conn_context -> connection_loop != NULL ) {
274
+ g_main_loop_quit (conn_context -> connection_loop );
275
+ g_main_loop_unref (conn_context -> connection_loop );
276
+ }
277
+ if (conn_context -> handler_id != 0 ) {
278
+ g_signal_handler_disconnect (conn_context -> device ,conn_context -> handler_id );
279
+ conn_context -> handler_id = 0 ;
280
+ }
257
281
282
+ free (conn_context );
258
283
free (connection -> context );
259
284
free (connection );
260
285
return GATTLIB_SUCCESS ;
0 commit comments