sindresorhus / Settings

⚙ Add a settings window to your macOS app in minutes
MIT License
1.43k stars 100 forks source link

Crashes on `viewDidLoad` due to `nil` outlets #80

Closed fmoraes74 closed 1 year ago

fmoraes74 commented 2 years ago

I have seen reports of crashes on viewDidLoad methods of the preference panes, like this:

Thread 0 Crashed:
0   HSTracker                            0x000000010b816eaf HSTracker.TrackersPreferences.viewDidLoad() -> () (TrackersPreferences.swift:0)
1   HSTracker                            0x000000010b816ef8 @objc HSTracker.TrackersPreferences.viewDidLoad() -> () (<compiler-generated>:0)
2   AppKit                               0x00007ff814505a92 -[NSViewController _sendViewDidLoad] + 86
3   AppKit                               0x00007ff8144eded2 -[NSViewController _loadViewIfRequired] + 384
4   AppKit                               0x00007ff8144edd16 -[NSViewController view] + 22
5   HSTracker                            0x000000010b99e2a3 Preferences.PreferencesTabViewController.(setWindowFrame in _1B0415CEB51F45D735E92B4C0504C08B)(for: __C.NSViewController, animated: Swift.Bool) -> () (PreferencesTabViewController.swift:193)
6   HSTracker                            0x000000010b99e7e3 closure #1 (__C.NSAnimationContext) -> () in Preferences.PreferencesTabViewController.transition(from: __C.NSViewController, to: __C.NSViewController, options: __C.NSViewControllerTransitionOptions, completionHandler: () -> ()?) -> () (PreferencesTabViewController.swift:169)
7   HSTracker                            0x000000010b99f089 partial apply forwarder for closure #1 (__C.NSAnimationContext) -> () in Preferences.PreferencesTabViewController.transition(from: __C.NSViewController, to: __C.NSViewController, options: __C.NSViewControllerTransitionOptions, completionHandler: () -> ()?) -> () (<compiler-generated>:0)
8   HSTracker                            0x000000010b99f0c1 partial apply forwarder for reabstraction thunk helper from @callee_guaranteed (@guaranteed __C.NSAnimationContext) -> () to @escaping @callee_guaranteed (@guaranteed __C.NSAnimationContext) -> () (<compiler-generated>:0)
9   HSTracker                            0x000000010b99e8e8 reabstraction thunk helper from @escaping @callee_guaranteed (@guaranteed __C.NSAnimationContext) -> () to @callee_unowned @convention(block) (@unowned __C.NSAnimationContext) -> () (<compiler-generated>:0)
10  AppKit                               0x00007ff8144b01ab +[NSAnimationContext runAnimationGroup:] + 54
11  HSTracker                            0x000000010b99dfed Preferences.PreferencesTabViewController.(animateTabTransition in _1B0415CEB51F45D735E92B4C0504C08B)(index: Swift.Int, animated: Swift.Bool) -> () (PreferencesTabViewController.swift:165)
12  HSTracker                            0x000000010b99da1f Preferences.PreferencesTabViewController.activateTab(index: Swift.Int, animated: Swift.Bool) -> () (PreferencesTabViewController.swift:85)
13  HSTracker                            0x000000010b9a45c3 Preferences.ToolbarItemStyleViewController.(toolbarItemSelected in _7A80F77958D94D970CD625FA09110540)(__C.NSToolbarItem) -> () (ToolbarItemStyleViewController.swift:0)
14  HSTracker                            0x000000010b9a461f @objc Preferences.ToolbarItemStyleViewController.(toolbarItemSelected in _7A80F77958D94D970CD625FA09110540)(__C.NSToolbarItem) -> () (<compiler-generated>:0)
15  AppKit                               0x00007ff8147f302f -[NSToolbarButton sendAction:to:] + 106
16  AppKit                               0x00007ff81479092e -[NSToolbarItemViewer mouseDown:] + 5172
17  AppKit                               0x00007ff814692bc1 -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 4858
18  AppKit                               0x00007ff814606d7e -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 2581
19  AppKit                               0x00007ff81460614e -[NSWindow(NSEventRouting) sendEvent:] + 351
20  HSTracker                            0x000000010b9a5429 @objc Preferences.UserInteractionPausableWindow.sendEvent(__C.NSEvent) -> () (Utilities.swift:92)
21  AppKit                               0x00007ff814604524 -[NSApplication(NSEvent) sendEvent:] + 351
22  AppKit                               0x00007ff8148bd18b -[NSApplication _handleEvent:] + 64
23  AppKit                               0x00007ff814485d3e -[NSApplication run] + 622
24  AppKit                               0x00007ff814459c97 NSApplicationMain + 816
25  HSTracker                            0x000000010b762fe9 main (AppDelegate.swift:21)
26  ???                                  0x000000011616152e 0x0 + 0

Any idea what could be causing it? From decoding the addresses, the crash location is in the first reference to an IBOutlet. This same pattern has showed up on different preference panes as well;

DivineDominion commented 1 year ago

@fmoraes74 This looks like the crash is happening in your view controllers, so it's out of scope of this package. Found your dev forums thread, which is a much better place: https://developer.apple.com/forums/thread/711847

fmoraes74 commented 1 year ago

The crash is because the outlets are not connected, not because of the actual code. Is it possible there is no check or assert in the Preferences code to make sure the NIB was actually loaded? I don't see how this can be the HSTracker code when it works all the time. If connections were missing, it would always fail. So the only explanation is that sometimes the NIB doesn't load correctly and the implicit references are actually nil and fail at runtime.