@@ -40,34 +40,37 @@ + (void)swizzleNSApplicationCrashOnException
4040# if SENTRY_TEST || SENTRY_TEST_CI
4141# pragma clang diagnostic ignored "-Wunused-variable"
4242# endif
43- // AppKit calls _crashOnException: when an exception is caught during CATransaction flush
44- // or view layout, bypassing reportException: entirely. Depending on macOS version, AppKit
45- // may call the class method +[NSApplication _crashOnException:] or the instance method
46- // -[NSApp _crashOnException:], so we swizzle both.
47- SEL selector = NSSelectorFromString (@" _crashOnException:" );
48-
49- SentrySwizzleClassMethod (NSApplication , selector, SentrySWReturnType (void ),
50- SentrySWArguments (NSException * exception), SentrySWReplacement ({
51- [SentryUncaughtNSExceptions capture: exception];
43+ static dispatch_once_t onceToken;
44+ dispatch_once (&onceToken, ^{
45+ // AppKit calls _crashOnException: when an exception is caught during CATransaction flush
46+ // or view layout, bypassing reportException: entirely. Depending on macOS version, AppKit
47+ // may call the class method +[NSApplication _crashOnException:] or the instance method
48+ // -[NSApp _crashOnException:], so we swizzle both.
49+ SEL selector = NSSelectorFromString (@" _crashOnException:" );
50+
51+ SentrySwizzleClassMethod (NSApplication , selector, SentrySWReturnType (void ),
52+ SentrySWArguments (NSException * exception), SentrySWReplacement ({
53+ [SentryUncaughtNSExceptions capture: exception];
5254# if SENTRY_TEST || SENTRY_TEST_CI
53- // Don't call the original in tests as it would abort() the process.
54- swizzleInfo.originalCalled = YES ;
55+ // Don't call the original in tests as it would abort() the process.
56+ swizzleInfo.originalCalled = YES ;
5557# else
56- return SentrySWCallOriginal (exception);
58+ return SentrySWCallOriginal (exception);
5759# endif
58- }));
60+ }));
5961
60- SentrySwizzleInstanceMethod (NSApplication , selector, SentrySWReturnType (void ),
61- SentrySWArguments (NSException * exception), SentrySWReplacement ({
62- [SentryUncaughtNSExceptions capture: exception];
62+ SentrySwizzleInstanceMethod (NSApplication , selector, SentrySWReturnType (void ),
63+ SentrySWArguments (NSException * exception), SentrySWReplacement ({
64+ [SentryUncaughtNSExceptions capture: exception];
6365# if SENTRY_TEST || SENTRY_TEST_CI
64- // Don't call the original in tests as it would abort() the process.
65- swizzleInfo.originalCalled = YES ;
66+ // Don't call the original in tests as it would abort() the process.
67+ swizzleInfo.originalCalled = YES ;
6668# else
67- return SentrySWCallOriginal (exception);
69+ return SentrySWCallOriginal (exception);
6870# endif
69- }),
70- SentrySwizzleModeOncePerClassAndSuperclasses, (void *)selector);
71+ }),
72+ SentrySwizzleModeOncePerClassAndSuperclasses, (void *)selector);
73+ });
7174# pragma clang diagnostic pop
7275}
7376
0 commit comments