Skip to content

Commit 12d8238

Browse files
committed
workbench: Fix crash when a monitored entry is removed
When a monitored file or directory is removed, we call the handler for that event directly. However, this might lead to removing our own monitoring entry, which means we must not reference it any further after having called the handler. Fixes #1430.
1 parent a8aaca9 commit 12d8238

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

workbench/src/wb_monitor.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,14 @@ static void wb_monitor_entry_free (gpointer data)
9090

9191
/* Callback function for file monitoring. */
9292
static void wb_monitor_file_changed_cb(G_GNUC_UNUSED GFileMonitor *monitor,
93-
G_GNUC_UNUSED GFile *file,
94-
G_GNUC_UNUSED GFile *other_file,
93+
GFile *file,
94+
GFile *other_file,
9595
GFileMonitorEvent event,
9696
WB_MONITOR_ENTRY *entry)
9797
{
9898
const gchar *event_string = NULL;
9999
gchar *file_path, *other_file_path = NULL;
100+
void (*handler) (WORKBENCH *, WB_PROJECT *, WB_PROJECT_DIR *, const gchar *) = NULL;
100101

101102
g_return_if_fail(entry != NULL);
102103

@@ -111,14 +112,12 @@ static void wb_monitor_file_changed_cb(G_GNUC_UNUSED GFileMonitor *monitor,
111112
{
112113
case G_FILE_MONITOR_EVENT_CREATED:
113114
event_string = "FILE_CREATED";
114-
workbench_process_add_file_event (wb_globals.opened_wb,
115-
entry->prj, entry->dir, file_path);
115+
handler = workbench_process_add_file_event;
116116
break;
117117

118118
case G_FILE_MONITOR_EVENT_DELETED:
119119
event_string = "FILE_DELETED";
120-
workbench_process_remove_file_event (wb_globals.opened_wb,
121-
entry->prj, entry->dir, file_path);
120+
handler = workbench_process_remove_file_event;
122121
break;
123122

124123
default:
@@ -131,6 +130,13 @@ static void wb_monitor_file_changed_cb(G_GNUC_UNUSED GFileMonitor *monitor,
131130
wb_project_dir_get_name(entry->dir), event_string, file_path);
132131
}
133132

133+
if (handler != NULL)
134+
{
135+
/* The handler might destroy the entry, so we mustn't reference it any
136+
* further after calling it */
137+
handler (wb_globals.opened_wb, entry->prj, entry->dir, file_path);
138+
}
139+
134140
g_free(file_path);
135141
g_free(other_file_path);
136142
}

0 commit comments

Comments
 (0)