Crash in UIImagePickerDelegate method after migrating project to Swift 5

I am getting a crash in UIImagePickerDelegate method - imagePickerController(_:didFinishPickingMediaWithInfo:) , after migrating the project to Swift 5. I am using this in class which is in an internal framework which we use through cocoapods. What I have noticed is with the Swift compiler code generation - optimization level set to No optimization , it works as intended with no crashes but with optimization it crashes. Any idea why it is happening ?. This is the crash log ,

Crashed: com.apple.main-thread
0  libswiftCore.dylib             0x10a056954 swift_getAssociatedTypeWitnessSlowImpl(swift::MetadataRequest, swift::TargetWitnessTable<swift::InProcess>*, swift::TargetMetadata<swift::InProcess> const*, swift::TargetProtocolRequirement<swift::InProcess> const*, swift::TargetProtocolRequirement<swift::InProcess> const*) + 212
1  libswiftCore.dylib             0x10a056954 swift_getAssociatedTypeWitnessSlowImpl(swift::MetadataRequest, swift::TargetWitnessTable<swift::InProcess>*, swift::TargetMetadata<swift::InProcess> const*, swift::TargetProtocolRequirement<swift::InProcess> const*, swift::TargetProtocolRequirement<swift::InProcess> const*) + 212
2  libswiftCore.dylib             0x10a055018 swift_getAssociatedTypeWitness + 176
3  libswiftCore.dylib             0x109eea038 static _SwiftNewtypeWrapper<>._forceBridgeFromObjectiveC(_:result:) + 78376
4  DubizzleHorizontal             0x105100350 protocol witness for static _ObjectiveCBridgeable._forceBridgeFromObjectiveC(_:result:) in conformance NSKeyValueChangeKey (<compiler-generated>)
5  libswiftCore.dylib             0x10a040fb4 _bridgeNonVerbatimFromObjectiveC<A>(_:_:_:) + 1164
6  libswiftCore.dylib             0x109df1edc _forceBridgeFromObjectiveC<A>(_:_:) + 119068
7  libswiftFoundation.dylib       0x10a3a17b0 closure #2 in static Dictionary._forceBridgeFromObjectiveC(_:result:) + 8572
8  libswiftFoundation.dylib       0x10a45e534 thunk for @callee_guaranteed (@in_guaranteed Any, @in_guaranteed Any, @unowned UnsafeMutablePointer<ObjCBool>) -> ()partial apply + 16480
9  libswiftFoundation.dylib       0x10a3a13bc thunk for @escaping @callee_guaranteed (@in_guaranteed Any, @in_guaranteed Any, @unowned UnsafeMutablePointer<ObjCBool>) -> () + 7560
10 CoreFoundation                 0x198f00bf4 -[__NSFrozenDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:] + 176
11 libswiftFoundation.dylib       0x10a3a0b18 static Dictionary._forceBridgeFromObjectiveC(_:result:) + 5348
12 libswiftFoundation.dylib       0x10a395f10 static Dictionary._unconditionallyBridgeFromObjectiveC(_:) + 3104
13 DubizzleHorizontal             0x1052cff18 @objc AttachmentSelector.imagePickerController(_:didFinishPickingMediaWithInfo:) (<compiler-generated>)
14 UIKitCore                      0x1c5c8d990 -[UIImagePickerController _imagePickerDidCompleteWithInfo:] + 128
15 UIKitCore                      0x1c5c8d284 __60-[UIImagePickerController didSelectMediaWithInfoDictionary:]_block_invoke + 44
16 libdispatch.dylib              0x198a516c8 _dispatch_call_block_and_release + 24
17 libdispatch.dylib              0x198a52484 _dispatch_client_callout + 16
18 libdispatch.dylib              0x198a31b34 _dispatch_main_queue_callback_4CF$VARIANT$armv81 + 1012
19 CoreFoundation                 0x198fa8df4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
20 CoreFoundation                 0x198fa3cbc __CFRunLoopRun + 1964
21 CoreFoundation                 0x198fa31f0 CFRunLoopRunSpecific + 436
22 GraphicsServices               0x19b21c584 GSEventRunModal + 100
23 UIKitCore                      0x1c6304c00 UIApplicationMain + 212
24 DubizzleHorizontal             0x104bb6490 main (main.m:16)
25 libdyld.dylib                  0x198a62bb4 start + 4

Also i found a bug here which seems related - [SR-10463] Crash in swift_getAssociatedTypeWitnessSlowImpl Β· Issue #52863 Β· apple/swift Β· GitHub? . Are these two related or am I missing something which is causing the crash. ?
Thanks

1 Like

Hi all. We're also experiencing a similar issue:

|#0|0x0000000112066d68 in swift_getAssociatedTypeWitnessSlowImpl(swift::MetadataRequest, swift::TargetWitnessTable<swift::InProcess>*, swift::TargetMetadata<swift::InProcess> const*, swift::TargetProtocolRequirement<swift::InProcess> const*, swift::TargetProtocolRequirement<swift::InProcess> const*) ()|
|---|---|
|#1|0x0000000112065308 in swift_getAssociatedTypeWitness ()|
|#2|0x0000000111ec43dd in static _SwiftNewtypeWrapper<>._conditionallyBridgeFromObjectiveC(_:result:) ()|
|#3|0x000000010794b1d4 in protocol witness for static _ObjectiveCBridgeable._conditionallyBridgeFromObjectiveC(_:result:) in conformance NSNotificationName ()|
|#4|0x000000010865f8b9 in protocol witness for static _ObjectiveCBridgeable._conditionallyBridgeFromObjectiveC(_:result:) in conformance UIFontDescriptorAttributeName ()|
|#5|0x000000011204e9b8 in _bridgeNonVerbatimFromObjectiveCConditional<A>(_:_:_:) ()|
|#6|0x0000000111db782d in _conditionallyBridgeFromObjectiveC<A>(_:_:) ()|
|#7|0x0000000111e0fbd9 in _SwiftDeferredNSDictionary.object(forKey:) ()|
|#8|0x0000000111ef31ca in @objc _SwiftDeferredNSDictionary.object(forKey:) ()|
|#9|0x00000001133ab24f in +[UIFontDescriptor _createMungledDictionary:] ()|
|#10|0x00000001133ac5e0 in -[UICTFontDescriptor fontDescriptorByAddingAttributes:] ()|
<...>

Here's a stack-trace right after our code. Moreover, we've noticed that SWIFT_COMPILATION_MODE setting affects this behavior in addition to SWIFT_OPTIMIZATION_LEVEL mentioned by @ravisankar.r. In our case the crash happens when the settings are Whole Module and -O respectively. Any other combination does not seem to produce a crash in our situation.

Any input would be highly appreciated.

UPD: That stack-trace originates from a method of UIFontDescriptor:
func addingAttributes(_ attributes: [UIFontDescriptor.AttributeName : Any] = [:]) -> UIFontDescriptor
and not UIImagePickerDelegate as of the original post in the thread.

You can use the xcode code hint to reimplement the imagePickerController(_:didFinishPickingMediaWithInfo:) method, which is a bit different from the original method.