Skip to content

Instantly share code, notes, and snippets.

@steipete
Created August 7, 2019 08:57
Show Gist options
  • Save steipete/133b7640c3f50f24678e0d9192f4b76e to your computer and use it in GitHub Desktop.
Save steipete/133b7640c3f50f24678e0d9192f4b76e to your computer and use it in GitHub Desktop.
HACK around FB6940492: Mac Catalyst: Crash when presenting view controllers. Call early.
/**
HACK around FB6940492: Mac Catalyst: Crash when presenting view controllers
'NSRangeException', reason: 'Cannot remove an observer <UINSSceneViewController 0x600003558790> for the key path "view.window.screen.contentLayoutRect" from <UINSSceneViewController 0x600003558790> because it is not registered as an observer.'
0 CoreFoundation 0x00007fff3b20f183 __exceptionPreprocess + 250
1 libobjc.A.dylib 0x00007fff71719b64 objc_exception_throw + 48
2 Foundation 0x00007fff3d8ca2aa -[NSObject(NSKeyValueObserverRegistration) _removeObserver:forProperty:] + 578
3 Foundation 0x00007fff3d8ca01b -[NSObject(NSKeyValueObserverRegistration) removeObserver:forKeyPath:] + 74
4 Foundation 0x00007fff3d8e2898 -[NSObject(NSKeyValueObserverRegistration) removeObserver:forKeyPath:context:] + 190
5 UIKitMacHelper 0x00007fff6c11e8ee -[UINSSceneViewController dealloc] + 298
6 Foundation 0x00007fff3d92f822 NSKVODeallocate + 172
7 AppKit 0x00007fff38518baa -[NSViewController release] + 119
8 AppKit 0x00007fff3869207e -[NSAutounbinder dealloc] + 44
9 libobjc.A.dylib 0x00007fff7172952a _ZN19AutoreleasePoolPage12releaseUntilEPP11objc_object + 134
10 libobjc.A.dylib 0x00007fff7170fc30 objc_autoreleasePoolPop + 175
11 FrontBoardServices 0x00007fff56e73255 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 228
12 FrontBoardServices 0x00007fff56e8a4bf __84-[FBSWorkspaceScenesClient _queue_invalidateScene:withTransitionContext:completion:]_block_invoke + 290
13 libdispatch.dylib 0x000000010dd9f84f _dispatch_client_callout + 8
14 libdispatch.dylib 0x000000010dda30a3 _dispatch_block_invoke_direct + 289
15 FrontBoardServices 0x00007fff56e59508 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 30
*/
void PSPDFHackEatNSRangeExceptionsInSceneViewControllerDeallocation(void) {
let clazz = NSClassFromString(@"UINSSceneViewController");
if (!clazz) { return; }
let selector = NSSelectorFromString(@"dealloc");
__block IMP originalIMP = pspdf_swizzleSelectorWithBlock(clazz, selector, ^(__unsafe_unretained id _self) {
@try {
((void (*)(id, SEL))originalIMP)(_self, selector);
} @catch (...) {}
});
}
@steipete
Copy link
Author

steipete commented Aug 7, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment