[PATCH weston 09/10] evdev: call evdev_device_destroy on failure

Peter Hutterer peter.hutterer at who-t.net
Tue Aug 6 18:04:48 PDT 2013


Avoid keeping keeping what needs to be freed in sync in multiple places,
make evdev_device_destroy do the right thing instead.
---
 src/evdev.c | 30 ++++++++++--------------------
 1 file changed, 10 insertions(+), 20 deletions(-)

diff --git a/src/evdev.c b/src/evdev.c
index bd3b025..7bacbd9 100644
--- a/src/evdev.c
+++ b/src/evdev.c
@@ -590,41 +590,29 @@ evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
 	device->devname = strdup(devname);
 
 	if (!evdev_handle_device(device)) {
-		if (device->dispatch)
-			device->dispatch->interface->destroy(dispatch);
-		if (device->mtdev)
-			mtdev_close_delete(device->mtdev);
-		free(device->devnode);
-		free(device->devname);
-		free(device);
+		evdev_device_destroy(device);
 		return EVDEV_UNHANDLED_DEVICE;
 	}
 
 	if (evdev_configure_device(device) == -1)
-		goto err1;
+		goto err;
 
 	/* If the dispatch was not set up use the fallback. */
 	if (device->dispatch == NULL)
 		device->dispatch = fallback_dispatch_create();
 	if (device->dispatch == NULL)
-		goto err1;
+		goto err;
 
 	device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
 					      WL_EVENT_READABLE,
 					      evdev_device_data, device);
 	if (device->source == NULL)
-		goto err2;
+		goto err;
 
 	return device;
 
-err2:
-	device->dispatch->interface->destroy(device->dispatch);
-err1:
-	if (device->mtdev)
-		mtdev_close_delete(device->mtdev);
-	free(device->devname);
-	free(device->devnode);
-	free(device);
+err:
+	evdev_device_destroy(device);
 	return NULL;
 }
 
@@ -637,8 +625,10 @@ evdev_device_destroy(struct evdev_device *device)
 	if (dispatch)
 		dispatch->interface->destroy(dispatch);
 
-	wl_event_source_remove(device->source);
-	wl_list_remove(&device->link);
+	if (device->source)
+		wl_event_source_remove(device->source);
+	if (!wl_list_empty(&device->link))
+		wl_list_remove(&device->link);
 	if (device->mtdev)
 		mtdev_close_delete(device->mtdev);
 	close(device->fd);
-- 
1.8.2.1



More information about the wayland-devel mailing list