为什么我的 FinderSync 扩展在启动前就崩溃了?
Why is my FinderSync extension crashing before it starts?
我的 FinderSync 扩展程序每次启动时都会崩溃:
Process: My Finder Sync [8364]
Path: /Applications/Company/Container.app/Contents/PlugIns/My Finder Sync.appex/Contents/MacOS/My Finder Sync
Identifier: com.company.Container.My-Finder-Sync
Version: [Set on build machine] ([Set on build machine])
Code Type: X86-64 (Native)
Parent Process: ??? [1]
Responsible: My Finder Sync [8364]
User ID: 1129915948
Date/Time: 2019-01-04 11:31:34.146 -0500
OS Version: Mac OS X 10.13.6 (17G4015)
Report Version: 12
Anonymous UUID: 013E2942-CED9-22FA-438A-E3D0BA89EB5C
Sleep/Wake UUID: D21EDE9D-237A-412A-8456-B96453886BAD
Time Awake Since Boot: 3200 seconds
Time Since Wake: 2600 seconds
System Integrity Protection: disabled
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Application Specific Information:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSDictionaryM setObject:forKey:]: object cannot be nil (key: 37990439-1DFE-4F0B-9865-6FC1D34A3BE4)'
terminating with uncaught exception of type NSException
abort() called
Application Specific Backtrace 1:
0 CoreFoundation 0x00007fff31eb523b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fff59147c76 objc_exception_throw + 48
2 CoreFoundation 0x00007fff31ef64c4 _CFThrowFormattedException + 202
3 CoreFoundation 0x00007fff31db87cc -[__NSDictionaryM setObject:forKey:] + 1020
4 Foundation 0x00007fff3408b348 -[_NSExtensionContextVendor _setPrincipalObject:forUUID:] + 106
5 Foundation 0x00007fff3408ab04 __105-[_NSExtensionContextVendor _beginRequestWithExtensionItems:listenerEndpoint:withContextUUID:completion:]_block_invoke + 804
6 libdispatch.dylib 0x00007fff59d2f5fa _dispatch_call_block_and_release + 12
7 libdispatch.dylib 0x00007fff59d27db8 _dispatch_client_callout + 8
8 libdispatch.dylib 0x00007fff59d33395 _dispatch_main_queue_callback_4CF + 1148
9 CoreFoundation 0x00007fff31e6db39 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
10 CoreFoundation 0x00007fff31e2fcda __CFRunLoopRun + 2586
11 CoreFoundation 0x00007fff31e2f033 CFRunLoopRunSpecific + 483
12 HIToolbox 0x00007fff31119d96 RunCurrentEventLoopInMode + 286
13 HIToolbox 0x00007fff31119b06 ReceiveNextEventCommon + 613
14 HIToolbox 0x00007fff31119884 _BlockUntilNextEventMatchingListInModeWithFilter + 64
15 AppKit 0x00007fff2f3c9a73 _DPSNextEvent + 2085
16 AppKit 0x00007fff2fb5fe34 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 3044
17 AppKit 0x00007fff2f3be885 -[NSApplication run] + 764
18 AppKit 0x00007fff2f38da72 NSApplicationMain + 804
19 libxpc.dylib 0x00007fff5a0bbd77 _xpc_objc_main + 580
20 libxpc.dylib 0x00007fff5a0ba9ca xpc_main + 433
21 Foundation 0x00007fff33f825f5 +[NSXPCListener serviceListener] + 0
22 PlugInKit 0x00007fff50ddbf8d -[PKService run] + 704
23 PlugInKit 0x00007fff50ddbb99 +[PKService main] + 55
24 PlugInKit 0x00007fff50ddbfb1 +[PKService _defaultRun:arguments:] + 17
25 Foundation 0x00007fff34130229 NSExtensionMain + 51
26 libdyld.dylib 0x00007fff59d61015 start + 1
27 ??? 0x0000000000000001 0x0 + 1
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib 0x00007fff59eb1b66 __pthread_kill + 10
1 libsystem_pthread.dylib 0x00007fff5a07c080 pthread_kill + 333
2 libsystem_c.dylib 0x00007fff59e0d1ae abort + 127
3 libc++abi.dylib 0x00007fff57d07f8f abort_message + 245
4 libc++abi.dylib 0x00007fff57d0812b default_terminate_handler() + 265
5 libobjc.A.dylib 0x00007fff59149ea3 _objc_terminate() + 97
6 libc++abi.dylib 0x00007fff57d237c9 std::__terminate(void (*)()) + 8
7 libc++abi.dylib 0x00007fff57d23843 std::terminate() + 51
8 libdispatch.dylib 0x00007fff59d27dcc _dispatch_client_callout + 28
9 libdispatch.dylib 0x00007fff59d33395 _dispatch_main_queue_callback_4CF + 1148
10 com.apple.CoreFoundation 0x00007fff31e6db39 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
11 com.apple.CoreFoundation 0x00007fff31e2fcda __CFRunLoopRun + 2586
12 com.apple.CoreFoundation 0x00007fff31e2f033 CFRunLoopRunSpecific + 483
13 com.apple.HIToolbox 0x00007fff31119d96 RunCurrentEventLoopInMode + 286
14 com.apple.HIToolbox 0x00007fff31119b06 ReceiveNextEventCommon + 613
15 com.apple.HIToolbox 0x00007fff31119884 _BlockUntilNextEventMatchingListInModeWithFilter + 64
16 com.apple.AppKit 0x00007fff2f3c9a73 _DPSNextEvent + 2085
17 com.apple.AppKit 0x00007fff2fb5fe34 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 3044
18 com.apple.AppKit 0x00007fff2f3be885 -[NSApplication run] + 764
19 com.apple.AppKit 0x00007fff2f38da72 NSApplicationMain + 804
20 libxpc.dylib 0x00007fff5a0bbd77 _xpc_objc_main + 580
21 libxpc.dylib 0x00007fff5a0ba9ca xpc_main + 433
22 com.apple.Foundation 0x00007fff33f825f5 -[NSXPCListener resume] + 165
23 com.apple.pluginkit.framework 0x00007fff50ddbf8d -[PKService run] + 704
24 com.apple.pluginkit.framework 0x00007fff50ddbb99 +[PKService main] + 55
25 com.apple.pluginkit.framework 0x00007fff50ddbfb1 +[PKService _defaultRun:arguments:] + 17
26 com.apple.Foundation 0x00007fff34130229 NSExtensionMain + 51
27 libdyld.dylib 0x00007fff59d61015 start + 1
Thread 1:
0 libsystem_kernel.dylib 0x00007fff59eb228a __workq_kernreturn + 10
1 libsystem_pthread.dylib 0x00007fff5a079009 _pthread_wqthread + 1035
2 libsystem_pthread.dylib 0x00007fff5a078be9 start_wqthread + 13
Thread 2:
0 libsystem_kernel.dylib 0x00007fff59eb228a __workq_kernreturn + 10
1 libsystem_pthread.dylib 0x00007fff5a079009 _pthread_wqthread + 1035
2 libsystem_pthread.dylib 0x00007fff5a078be9 start_wqthread + 13
Thread 3:
0 libsystem_kernel.dylib 0x00007fff59eb228a __workq_kernreturn + 10
1 libsystem_pthread.dylib 0x00007fff5a079009 _pthread_wqthread + 1035
2 libsystem_pthread.dylib 0x00007fff5a078be9 start_wqthread + 13
Thread 4:
0 libsystem_pthread.dylib 0x00007fff5a078bdc start_wqthread + 0
1 ??? 0x0004000200000006 0 + 1125908496777222
Thread 5:: com.apple.NSEventThread
0 libsystem_kernel.dylib 0x00007fff59ea820a mach_msg_trap + 10
1 libsystem_kernel.dylib 0x00007fff59ea7724 mach_msg + 60
2 com.apple.CoreFoundation 0x00007fff31e30665 __CFRunLoopServiceMachPort + 341
3 com.apple.CoreFoundation 0x00007fff31e2f9b7 __CFRunLoopRun + 1783
4 com.apple.CoreFoundation 0x00007fff31e2f033 CFRunLoopRunSpecific + 483
5 com.apple.AppKit 0x00007fff2f506fc4 _NSEventThread + 184
6 libsystem_pthread.dylib 0x00007fff5a079661 _pthread_body + 340
7 libsystem_pthread.dylib 0x00007fff5a07950d _pthread_start + 377
8 libsystem_pthread.dylib 0x00007fff5a078bf9 thread_start + 13
Thread 0 crashed with X86 Thread State (64-bit):
rax: 0x0000000000000000 rbx: 0x00007fff927a7380 rcx: 0x00007ffee31a6c68 rdx: 0x0000000000000000
rdi: 0x0000000000000307 rsi: 0x0000000000000006 rbp: 0x00007ffee31a6ca0 rsp: 0x00007ffee31a6c68
r8: 0x00007ffee31a6b30 r9: 0x00007ffee31a6d00 r10: 0x0000000000000000 r11: 0x0000000000000206
r12: 0x0000000000000307 r13: 0x0000000000000030 r14: 0x0000000000000006 r15: 0x000000000000002d
rip: 0x00007fff59eb1b66 rfl: 0x0000000000000206 cr2: 0x00007fff92784168
如您所见,其中只有 Apple 平台的内容。更糟糕的是,这种情况发生在 在 调用我的任何代码之前!至少,它从来没有在我的 FIFinderSync
class' init
.
中遇到我的断点
这是怎么回事?为什么它会因某些字典中的 UUID 键和 nil
值而崩溃?我已经检查了我所有的代码并确保不会发生(我确实有一些这样的字典,但都在 Swift 代码中并且值不是可选的,所以永远不会是 nil
;没关系,他们的断点也没有命中)。
我的猜测是您的扩展程序 Info.plist 中的某些内容有误。也许那里缺少一个值,当 OS 试图获取它的值时,它返回 nil。当异常发生时,它可能会尝试将该值放入另一个字典。从所涉及函数的名称来看,可能是您的 Principal Class 或 Info.plist 文件中缺少的类似条目。
Swift 进行一些名称修改,即使看起来不像。当您通过字符串引用 class 名称时,就像在 FinderSync 扩展的 info.plist
中一样,这并不总是像 Swift.
中预期的那样工作
例如,如果您有一个 class 像:
@objc
class MyFinderSyncPrinicpalClass: FIFinderSync {
// ...
}
尝试编译它并查看生成的 Obj-C header。它可能看起来像这样:
SWIFT_CLASS("_TtC17My_Finder_Sync10MyFinderSyncPrinicpalClass")
@interface MyFinderSyncPrinicpalClass : FIFinderSync
// ...
@end
这意味着 Swift 和 Objective-C 代码都可以简单地将其引用为 MyFinderSyncPrinicpalClass
,但是任何想要通过字符串引用其编译形式的东西,例如反射或那个 info.plist
,看到那个损坏的名字 _TtC17My_Finder_Sync10MyFinderSyncPrinicpalClass
。要解决此问题,只需在 @objc
注释中明确指定名称:
@objc(MyFinderSyncPrinicpalClass)
class MyFinderSyncPrinicpalClass: FIFinderSync {
// ...
}
现在如果你compare that to the newly-generated Obj-C header,你就会明白我的意思了:
SWIFT_CLASS("MyFinderSyncPrinicpalClass")
@interface MyFinderSyncPrinicpalClass : FIFinderSync
// ...
@end
现在您可以在 info.plist
中使用它的实际名称来引用它,它将按预期启动!
我的 FinderSync 扩展程序每次启动时都会崩溃:
Process: My Finder Sync [8364]
Path: /Applications/Company/Container.app/Contents/PlugIns/My Finder Sync.appex/Contents/MacOS/My Finder Sync
Identifier: com.company.Container.My-Finder-Sync
Version: [Set on build machine] ([Set on build machine])
Code Type: X86-64 (Native)
Parent Process: ??? [1]
Responsible: My Finder Sync [8364]
User ID: 1129915948
Date/Time: 2019-01-04 11:31:34.146 -0500
OS Version: Mac OS X 10.13.6 (17G4015)
Report Version: 12
Anonymous UUID: 013E2942-CED9-22FA-438A-E3D0BA89EB5C
Sleep/Wake UUID: D21EDE9D-237A-412A-8456-B96453886BAD
Time Awake Since Boot: 3200 seconds
Time Since Wake: 2600 seconds
System Integrity Protection: disabled
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Application Specific Information:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSDictionaryM setObject:forKey:]: object cannot be nil (key: 37990439-1DFE-4F0B-9865-6FC1D34A3BE4)'
terminating with uncaught exception of type NSException
abort() called
Application Specific Backtrace 1:
0 CoreFoundation 0x00007fff31eb523b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fff59147c76 objc_exception_throw + 48
2 CoreFoundation 0x00007fff31ef64c4 _CFThrowFormattedException + 202
3 CoreFoundation 0x00007fff31db87cc -[__NSDictionaryM setObject:forKey:] + 1020
4 Foundation 0x00007fff3408b348 -[_NSExtensionContextVendor _setPrincipalObject:forUUID:] + 106
5 Foundation 0x00007fff3408ab04 __105-[_NSExtensionContextVendor _beginRequestWithExtensionItems:listenerEndpoint:withContextUUID:completion:]_block_invoke + 804
6 libdispatch.dylib 0x00007fff59d2f5fa _dispatch_call_block_and_release + 12
7 libdispatch.dylib 0x00007fff59d27db8 _dispatch_client_callout + 8
8 libdispatch.dylib 0x00007fff59d33395 _dispatch_main_queue_callback_4CF + 1148
9 CoreFoundation 0x00007fff31e6db39 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
10 CoreFoundation 0x00007fff31e2fcda __CFRunLoopRun + 2586
11 CoreFoundation 0x00007fff31e2f033 CFRunLoopRunSpecific + 483
12 HIToolbox 0x00007fff31119d96 RunCurrentEventLoopInMode + 286
13 HIToolbox 0x00007fff31119b06 ReceiveNextEventCommon + 613
14 HIToolbox 0x00007fff31119884 _BlockUntilNextEventMatchingListInModeWithFilter + 64
15 AppKit 0x00007fff2f3c9a73 _DPSNextEvent + 2085
16 AppKit 0x00007fff2fb5fe34 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 3044
17 AppKit 0x00007fff2f3be885 -[NSApplication run] + 764
18 AppKit 0x00007fff2f38da72 NSApplicationMain + 804
19 libxpc.dylib 0x00007fff5a0bbd77 _xpc_objc_main + 580
20 libxpc.dylib 0x00007fff5a0ba9ca xpc_main + 433
21 Foundation 0x00007fff33f825f5 +[NSXPCListener serviceListener] + 0
22 PlugInKit 0x00007fff50ddbf8d -[PKService run] + 704
23 PlugInKit 0x00007fff50ddbb99 +[PKService main] + 55
24 PlugInKit 0x00007fff50ddbfb1 +[PKService _defaultRun:arguments:] + 17
25 Foundation 0x00007fff34130229 NSExtensionMain + 51
26 libdyld.dylib 0x00007fff59d61015 start + 1
27 ??? 0x0000000000000001 0x0 + 1
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib 0x00007fff59eb1b66 __pthread_kill + 10
1 libsystem_pthread.dylib 0x00007fff5a07c080 pthread_kill + 333
2 libsystem_c.dylib 0x00007fff59e0d1ae abort + 127
3 libc++abi.dylib 0x00007fff57d07f8f abort_message + 245
4 libc++abi.dylib 0x00007fff57d0812b default_terminate_handler() + 265
5 libobjc.A.dylib 0x00007fff59149ea3 _objc_terminate() + 97
6 libc++abi.dylib 0x00007fff57d237c9 std::__terminate(void (*)()) + 8
7 libc++abi.dylib 0x00007fff57d23843 std::terminate() + 51
8 libdispatch.dylib 0x00007fff59d27dcc _dispatch_client_callout + 28
9 libdispatch.dylib 0x00007fff59d33395 _dispatch_main_queue_callback_4CF + 1148
10 com.apple.CoreFoundation 0x00007fff31e6db39 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
11 com.apple.CoreFoundation 0x00007fff31e2fcda __CFRunLoopRun + 2586
12 com.apple.CoreFoundation 0x00007fff31e2f033 CFRunLoopRunSpecific + 483
13 com.apple.HIToolbox 0x00007fff31119d96 RunCurrentEventLoopInMode + 286
14 com.apple.HIToolbox 0x00007fff31119b06 ReceiveNextEventCommon + 613
15 com.apple.HIToolbox 0x00007fff31119884 _BlockUntilNextEventMatchingListInModeWithFilter + 64
16 com.apple.AppKit 0x00007fff2f3c9a73 _DPSNextEvent + 2085
17 com.apple.AppKit 0x00007fff2fb5fe34 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 3044
18 com.apple.AppKit 0x00007fff2f3be885 -[NSApplication run] + 764
19 com.apple.AppKit 0x00007fff2f38da72 NSApplicationMain + 804
20 libxpc.dylib 0x00007fff5a0bbd77 _xpc_objc_main + 580
21 libxpc.dylib 0x00007fff5a0ba9ca xpc_main + 433
22 com.apple.Foundation 0x00007fff33f825f5 -[NSXPCListener resume] + 165
23 com.apple.pluginkit.framework 0x00007fff50ddbf8d -[PKService run] + 704
24 com.apple.pluginkit.framework 0x00007fff50ddbb99 +[PKService main] + 55
25 com.apple.pluginkit.framework 0x00007fff50ddbfb1 +[PKService _defaultRun:arguments:] + 17
26 com.apple.Foundation 0x00007fff34130229 NSExtensionMain + 51
27 libdyld.dylib 0x00007fff59d61015 start + 1
Thread 1:
0 libsystem_kernel.dylib 0x00007fff59eb228a __workq_kernreturn + 10
1 libsystem_pthread.dylib 0x00007fff5a079009 _pthread_wqthread + 1035
2 libsystem_pthread.dylib 0x00007fff5a078be9 start_wqthread + 13
Thread 2:
0 libsystem_kernel.dylib 0x00007fff59eb228a __workq_kernreturn + 10
1 libsystem_pthread.dylib 0x00007fff5a079009 _pthread_wqthread + 1035
2 libsystem_pthread.dylib 0x00007fff5a078be9 start_wqthread + 13
Thread 3:
0 libsystem_kernel.dylib 0x00007fff59eb228a __workq_kernreturn + 10
1 libsystem_pthread.dylib 0x00007fff5a079009 _pthread_wqthread + 1035
2 libsystem_pthread.dylib 0x00007fff5a078be9 start_wqthread + 13
Thread 4:
0 libsystem_pthread.dylib 0x00007fff5a078bdc start_wqthread + 0
1 ??? 0x0004000200000006 0 + 1125908496777222
Thread 5:: com.apple.NSEventThread
0 libsystem_kernel.dylib 0x00007fff59ea820a mach_msg_trap + 10
1 libsystem_kernel.dylib 0x00007fff59ea7724 mach_msg + 60
2 com.apple.CoreFoundation 0x00007fff31e30665 __CFRunLoopServiceMachPort + 341
3 com.apple.CoreFoundation 0x00007fff31e2f9b7 __CFRunLoopRun + 1783
4 com.apple.CoreFoundation 0x00007fff31e2f033 CFRunLoopRunSpecific + 483
5 com.apple.AppKit 0x00007fff2f506fc4 _NSEventThread + 184
6 libsystem_pthread.dylib 0x00007fff5a079661 _pthread_body + 340
7 libsystem_pthread.dylib 0x00007fff5a07950d _pthread_start + 377
8 libsystem_pthread.dylib 0x00007fff5a078bf9 thread_start + 13
Thread 0 crashed with X86 Thread State (64-bit):
rax: 0x0000000000000000 rbx: 0x00007fff927a7380 rcx: 0x00007ffee31a6c68 rdx: 0x0000000000000000
rdi: 0x0000000000000307 rsi: 0x0000000000000006 rbp: 0x00007ffee31a6ca0 rsp: 0x00007ffee31a6c68
r8: 0x00007ffee31a6b30 r9: 0x00007ffee31a6d00 r10: 0x0000000000000000 r11: 0x0000000000000206
r12: 0x0000000000000307 r13: 0x0000000000000030 r14: 0x0000000000000006 r15: 0x000000000000002d
rip: 0x00007fff59eb1b66 rfl: 0x0000000000000206 cr2: 0x00007fff92784168
如您所见,其中只有 Apple 平台的内容。更糟糕的是,这种情况发生在 在 调用我的任何代码之前!至少,它从来没有在我的 FIFinderSync
class' init
.
这是怎么回事?为什么它会因某些字典中的 UUID 键和 nil
值而崩溃?我已经检查了我所有的代码并确保不会发生(我确实有一些这样的字典,但都在 Swift 代码中并且值不是可选的,所以永远不会是 nil
;没关系,他们的断点也没有命中)。
我的猜测是您的扩展程序 Info.plist 中的某些内容有误。也许那里缺少一个值,当 OS 试图获取它的值时,它返回 nil。当异常发生时,它可能会尝试将该值放入另一个字典。从所涉及函数的名称来看,可能是您的 Principal Class 或 Info.plist 文件中缺少的类似条目。
Swift 进行一些名称修改,即使看起来不像。当您通过字符串引用 class 名称时,就像在 FinderSync 扩展的 info.plist
中一样,这并不总是像 Swift.
例如,如果您有一个 class 像:
@objc
class MyFinderSyncPrinicpalClass: FIFinderSync {
// ...
}
尝试编译它并查看生成的 Obj-C header。它可能看起来像这样:
SWIFT_CLASS("_TtC17My_Finder_Sync10MyFinderSyncPrinicpalClass")
@interface MyFinderSyncPrinicpalClass : FIFinderSync
// ...
@end
这意味着 Swift 和 Objective-C 代码都可以简单地将其引用为 MyFinderSyncPrinicpalClass
,但是任何想要通过字符串引用其编译形式的东西,例如反射或那个 info.plist
,看到那个损坏的名字 _TtC17My_Finder_Sync10MyFinderSyncPrinicpalClass
。要解决此问题,只需在 @objc
注释中明确指定名称:
@objc(MyFinderSyncPrinicpalClass)
class MyFinderSyncPrinicpalClass: FIFinderSync {
// ...
}
现在如果你compare that to the newly-generated Obj-C header,你就会明白我的意思了:
SWIFT_CLASS("MyFinderSyncPrinicpalClass")
@interface MyFinderSyncPrinicpalClass : FIFinderSync
// ...
@end
现在您可以在 info.plist
中使用它的实际名称来引用它,它将按预期启动!