From 2e280b62bcd28a60c31eb998a708879897d38258 Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Thu, 28 May 2020 16:58:22 +0200 Subject: [PATCH] #543 resolve issue with lifetime of NSRunningApplication --- src/process_manager.c | 3 ++- src/workspace.h | 2 +- src/workspace.m | 15 +++++++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/process_manager.c b/src/process_manager.c index 8df1ea06..af7b8acf 100644 --- a/src/process_manager.c +++ b/src/process_manager.c @@ -1,6 +1,7 @@ #include "process_manager.h" extern struct event_loop g_event_loop; +extern void *g_workspace_context; static TABLE_HASH_FUNC(hash_psn) { @@ -40,7 +41,7 @@ struct process *process_create(ProcessSerialNumber psn) void process_destroy(struct process *process) { - workspace_application_destroy_running_ns_application(process); + workspace_application_destroy_running_ns_application(g_workspace_context, process); free(process->name); free(process); } diff --git a/src/workspace.h b/src/workspace.h index ae339c90..f3cd3dc4 100644 --- a/src/workspace.h +++ b/src/workspace.h @@ -12,7 +12,7 @@ void workspace_event_handler_end(void *context); struct process; void *workspace_application_create_running_ns_application(struct process *process); -void workspace_application_destroy_running_ns_application(struct process *process); +void workspace_application_destroy_running_ns_application(void *context, struct process *process); bool workspace_application_is_observable(struct process *process); bool workspace_application_is_finished_launching(struct process *process); void workspace_application_observe_finished_launching(void *context, struct process *process); diff --git a/src/workspace.m b/src/workspace.m index c0e5487f..70ccb776 100644 --- a/src/workspace.m +++ b/src/workspace.m @@ -25,10 +25,21 @@ void workspace_event_handler_end(void *context) return [NSRunningApplication runningApplicationWithProcessIdentifier:process->pid]; } -void workspace_application_destroy_running_ns_application(struct process *process) +void workspace_application_destroy_running_ns_application(void *context, struct process *process) { NSRunningApplication *application = process->ns_application; - if (application) [application release]; + + if (application) { + @try { + [application removeObserver:context forKeyPath:@"activationPolicy"]; + } @catch (NSException * __unused exception) {} + + @try { + [application removeObserver:context forKeyPath:@"finishedLaunching"]; + } @catch (NSException * __unused exception) {} + + [application release]; + } } void workspace_application_observe_finished_launching(void *context, struct process *process)