Skip to content

Commit

Permalink
controller: Remove event controllers on dispose
Browse files Browse the repository at this point in the history
Events may fire after dispose otherwise, and the handlers could try to
access objects that are already freed.

Fixes #981.
  • Loading branch information
gnome-mpv committed Sep 30, 2024
1 parent c7d160c commit 3f2aef9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
29 changes: 22 additions & 7 deletions src/celluloid-controller-input.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ celluloid_controller_input_connect_signals(CelluloidController *controller)
G_CALLBACK(key_released_handler),
controller );

GtkGesture *click_gesture =
gtk_gesture_click_new();
GtkGesture *const click_gesture =
controller->click_gesture;
gtk_gesture_single_set_button
(GTK_GESTURE_SINGLE(click_gesture), 0);
gtk_widget_add_controller
Expand All @@ -365,8 +365,8 @@ celluloid_controller_input_connect_signals(CelluloidController *controller)
G_CALLBACK(button_stopped_handler),
controller );

GtkEventController *motion_controller =
gtk_event_controller_motion_new();
GtkEventController *const motion_controller =
controller->motion_controller;
gtk_widget_add_controller
(GTK_WIDGET(video_area), GTK_EVENT_CONTROLLER(motion_controller));

Expand All @@ -375,9 +375,8 @@ celluloid_controller_input_connect_signals(CelluloidController *controller)
G_CALLBACK(mouse_move_handler),
controller );

GtkEventController *scroll_controller =
gtk_event_controller_scroll_new
(GTK_EVENT_CONTROLLER_SCROLL_BOTH_AXES | GTK_EVENT_CONTROLLER_SCROLL_DISCRETE);
GtkEventController *const scroll_controller =
controller->scroll_controller;
gtk_widget_add_controller
(GTK_WIDGET(video_area), GTK_EVENT_CONTROLLER(scroll_controller));

Expand All @@ -386,3 +385,19 @@ celluloid_controller_input_connect_signals(CelluloidController *controller)
G_CALLBACK(scroll_handler),
controller );
}

void
celluloid_controller_input_disconnect_signals(CelluloidController *controller)
{
CelluloidMainWindow *wnd =
celluloid_view_get_main_window(controller->view);
GtkWidget *video_area =
GTK_WIDGET(celluloid_main_window_get_video_area(wnd));

gtk_widget_remove_controller
(video_area, controller->motion_controller);
gtk_widget_remove_controller
(video_area, controller->scroll_controller);
gtk_widget_remove_controller
(video_area, GTK_EVENT_CONTROLLER(controller->click_gesture));
}
3 changes: 3 additions & 0 deletions src/celluloid-controller-input.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ G_BEGIN_DECLS
void
celluloid_controller_input_connect_signals(CelluloidController *controller);

void
celluloid_controller_input_disconnect_signals(CelluloidController *controller);

G_END_DECLS

#endif
3 changes: 3 additions & 0 deletions src/celluloid-controller-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ struct _CelluloidController
GObject parent;

GtkEventController *key_controller;
GtkEventController *motion_controller;
GtkEventController *scroll_controller;
GtkGesture *click_gesture;

CelluloidApplication *app;
CelluloidModel *model;
Expand Down
19 changes: 18 additions & 1 deletion src/celluloid-controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ dispose(GObject *object)

if(controller->view)
{
gtk_widget_remove_controller
( GTK_WIDGET(controller->view),
controller->key_controller );

celluloid_controller_input_disconnect_signals(controller);

celluloid_view_make_gl_context_current(controller->view);
g_clear_object(&controller->model);

Expand Down Expand Up @@ -1395,7 +1401,18 @@ celluloid_controller_class_init(CelluloidControllerClass *klass)
static void
celluloid_controller_init(CelluloidController *controller)
{
controller->key_controller = gtk_event_controller_key_new();
const GtkEventControllerScrollFlags scroll_flags =
GTK_EVENT_CONTROLLER_SCROLL_BOTH_AXES |
GTK_EVENT_CONTROLLER_SCROLL_DISCRETE;

controller->key_controller =
gtk_event_controller_key_new();
controller->motion_controller =
gtk_event_controller_motion_new();
controller->scroll_controller =
gtk_event_controller_scroll_new(scroll_flags);
controller->click_gesture =
gtk_gesture_click_new();

gtk_event_controller_set_propagation_phase
(controller->key_controller, GTK_PHASE_CAPTURE);
Expand Down

0 comments on commit 3f2aef9

Please sign in to comment.