EXC_BAD_ACCESS on application start

422 Views Asked by At

In my macOS application I have a very rare crash which I cannot reproduce and I can't find out what causes it.

Please see the call stack below.

Here are my thoughts:

It seems to happen on application start because there's NSPersistentUIRestorer in the call stack.

Then main window view (NSThemeFrame) changes its frame size and sends NSViewGeometryInWindowDidChangeNotification to the children.

Some button tries to setup mouse tracking with _setMouseTrackingForCell and crashes becase some subview (or superview?) is deallocated.

Right now I'm out of ideas and I would appreciate any advice or suggestion. Thank you in advance.

Call stack:

EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x00006080044e6820
Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x7fff50eb4ea9 objc_msgSend + 41
1  AppKit                         0x7fff272c62d5 -[NSView(NSInternal) _uninstallTrackingArea:] + 326
2  AppKit                         0x7fff2726525e -[NSView removeTrackingArea:] + 312
3  AppKit                         0x7fff2729d3c7 -[NSCell(NSPrivate_CellMouseTracking) _setMouseTrackingInRect:ofView:withConfiguration:] + 101
4  AppKit                         0x7fff2729d2fe -[NSCell(NSPrivate_CellMouseTracking) _setMouseTrackingInRect:ofView:] + 90
5  AppKit                         0x7fff2729d1bb -[NSButtonCell _setMouseTrackingInRect:ofView:] + 84
6  AppKit                         0x7fff2729d15c -[NSControl _setMouseTrackingForCell:] + 102
7  AppKit                         0x7fff2729d0af -[NSButton(NSButtonBorder) _setMouseTrackingForCell:] + 47
8  CoreFoundation                 0x7fff29ca5edc __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
9  CoreFoundation                 0x7fff29ca5daa _CFXRegistrationPost + 458
10 CoreFoundation                 0x7fff29ca5ae1 ___CFXNotificationPost_block_invoke + 225
11 CoreFoundation                 0x7fff29c63880 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1664
12 CoreFoundation                 0x7fff29c629b7 _CFXNotificationPost + 599
13 Foundation                     0x7fff2bd728c7 -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
14 AppKit                         0x7fff27ad9859 NSViewHierarchyNoteGeometryInWindowDidChange + 160
15 AppKit                         0x7fff27ad9a3a NSViewHierarchyNoteGeometryInWindowDidChange + 641
16 AppKit                         0x7fff27ad9a3a NSViewHierarchyNoteGeometryInWindowDidChange + 641
17 AppKit                         0x7fff27ad9a3a NSViewHierarchyNoteGeometryInWindowDidChange + 641
18 AppKit                         0x7fff27ad9a3a NSViewHierarchyNoteGeometryInWindowDidChange + 641
19 AppKit                         0x7fff27ad9a3a NSViewHierarchyNoteGeometryInWindowDidChange + 641
20 AppKit                         0x7fff2720e9a7 -[NSView _invalidateFocus] + 63
21 AppKit                         0x7fff2724e7dc -[NSView setFrameSize:] + 1902
22 AppKit                         0x7fff27264b1e -[NSView setFrame:] + 371
23 AppKit                         0x7fff2726f49c -[NSThemeFrame _relayoutAuxiliaryViewsOfType:] + 163
24 AppKit                         0x7fff2726f3e6 -[NSThemeFrame relayoutAuxiliaryViewsOfType:] + 27
25 AppKit                         0x7fff2726ec33 -[NSTitlebarViewController insertChildViewController:atIndex:] + 386
26 AppKit                         0x7fff27a88caa -[NSWindowStackController _makeTabBarForWindow:visible:] + 221
27 AppKit                         0x7fff27a8bc77 -[NSWindowStackController _addSyncedTabBarItemForWindow:atIndex:] + 625
28 AppKit                         0x7fff27a89b96 -[NSWindowStackController insertWindow:atIndex:] + 628
29 AppKit                         0x7fff27a8990b -[NSWindowStackController addWindow:] + 437
30 AppKit                         0x7fff276180ff -[NSWindow(NSWindowTabbing) _restoreTabbedWindowStateWithCoder:] + 299
31 AppKit                         0x7fff27533c82 -[NSWindow restoreStateWithCoder:] + 96
32 AppKit                         0x7fff274fcf17 restorePersistentStateWithWindowRestoration + 1004
33 AppKit                         0x7fff27533be2 -[NSPersistentUIRestorer invokeRestoration:] + 572
34 AppKit                         0x7fff27533947 __79-[NSPersistentUIRestorer finishedRestoringWindowsWithZOrder:completionHandler:]_block_invoke + 194
35 AppKit                         0x7fff27533723 +[NSWindow _batchMinimizeWindowsWithBlock:] + 86
36 AppKit                         0x7fff274fd475 -[NSPersistentUIRestorer finishedRestoringWindowsWithZOrder:completionHandler:] + 705
37 AppKit                         0x7fff27532a79 __82-[NSPersistentUIRestorer restoreStateFromRecords:usingDelegate:completionHandler:]_block_invoke_3 + 168
38 AppKit                         0x7fff2753298e __99-[NSApplication(NSPersistentUIRestorationSupport) _restoreWindowWithRestoration:completionHandler:]_block_invoke + 365
39 AppKit                         0x7fff27cb94e3 -[NSDocument(NSPersistentUISupport) restoreDocumentWindowWithIdentifier:state:completionHandler:] + 497
40 AppKit                         0x7fff2777d3c4 -[NSDocumentControllerPersistentRestoration loadedDocument:forAutoID:] + 176
41 AppKit                         0x7fff27782221 __89-[NSDocumentController reopenDocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_2 + 185
42 AppKit                         0x7fff27790f70 ___NSMainRunLoopPerformBlock_block_invoke + 25
43 CoreFoundation                 0x7fff29cae87c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
44 CoreFoundation                 0x7fff29c91253 __CFRunLoopDoBlocks + 275
45 CoreFoundation                 0x7fff29c91018 __CFRunLoopRun + 3128
46 CoreFoundation                 0x7fff29c90153 CFRunLoopRunSpecific + 483
47 HIToolbox                      0x7fff28f7ad96 RunCurrentEventLoopInMode + 286
48 HIToolbox                      0x7fff28f7ab06 ReceiveNextEventCommon + 613
49 HIToolbox                      0x7fff28f7a884 _BlockUntilNextEventMatchingListInModeWithFilter + 64
50 AppKit                         0x7fff2722ba73 _DPSNextEvent + 2085
51 AppKit                         0x7fff279c1e34 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 3044
52 AppKit                         0x7fff27220885 -[NSApplication run] + 764
53 AppKit                         0x7fff271efa72 NSApplicationMain + 804
54 MyApp                          0x106be1479 main (AppDelegate.swift:17)
55 libdyld.dylib                  0x7fff51adc015 start + 1

Update 1:

Running application with zombies shows the following warnings in the output

objc[67272]: Class _NSZombie_CFReadStream is implemented in both ?? (0x6040000bb290) and ?? (0x6040000b8150). One of the two will be used. Which one is undefined.
objc[67272]: Class _NSZombie_NSMachPort is implemented in both ?? (0x604000151990) and ?? (0x6040001532d0). One of the two will be used. Which one is undefined.
objc[67272]: Class _NSZombie_CFMachPort is implemented in both ?? (0x604000157110) and ?? (0x60400015c0d0). One of the two will be used. Which one is undefined.
objc[67272]: Class _NSZombie_NSConcreteTask is implemented in both ?? (0x604000157250) and ?? (0x604000153a50). One of the two will be used. Which one is undefined.
objc[67272]: Class _NSZombie_NSConcreteFileHandle is implemented in both ?? (0x60400016f450) and ?? (0x604000170c90). One of the two will be used. Which one is undefined.
objc[67272]: Class _NSZombie_NSConcreteFileHandle is implemented in both ?? (0x60400016f450) and ?? (0x6040001724d0). One of the two will be used. Which one is undefined.

Update 2: I managed to reproduce the exact call stack using symbolic break points (but still have never seen the actual crash).

Then using LLDB I requested the information about the button and found out that it is just a regular button with recessed style. Not sure why it causes crash sometimes. May be it's just the first leaf in view hierarchy tree.

For curious ones, here's how I got that information:

(lldb) expr -l Swift -- import Cocoa
(lldb) po $arg1
<NSButton: 0x60f0000ebdb0>
(lldb) expr -l Swift -- let $btn = unsafeBitCast(0x60f0000f7660, to: NSButton.self)
(lldb) expr -l Swift -- print($btn.title)
0

There are 0 best solutions below