Fatal Exception: NSInvalidArgumentException -[__SwiftValue _fastCStringContents:]: unrecognized selector sent to instance

Hi! We've been encountering crashes like this in iOS 13 devices. On the stack trace, it appears that the NSError object is expecting a string description but got a different type. I want to confirm if my understanding on the stack trace is right and if not, can anyone help me? Or anyone has encountered the same crash.
Crash Key: CoreUI: deallocating _CUIInternalLinkRendition 1855 /System/Library/CoreServices/CoreGlyphs.bundle/Assets.car


Stack Trace:

Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x19f82498c __exceptionPreprocess
1  libobjc.A.dylib                0x19f54d0a4 objc_exception_throw
2  CoreFoundation                 0x19f72843c -[NSOrderedSet initWithSet:copyItems:]
3  CoreFoundation                 0x19f828e08 ___forwarding___
4  CoreFoundation                 0x19f82abec _CF_forwarding_prep_0
5  libswiftCore.dylib             0x1acabf4f0 _bridgeCocoaString(_:)
6  libswiftCore.dylib             0x1acabf6bc String.init(_cocoaString:)
7  libswiftFoundation.dylib       0x1d385458c Error.localizedDescription.getter
8  OjO                            0x1046552c4 closure #8 in EnterCreditCardViewController.setObservables() + 199 (EnterCreditCardViewController.swift:199)
9  OjO                            0x10467bcec thunk for @escaping @callee_guaranteed (@guaranteed Error?) -> () (<compiler-generated>)
10 RxSwift                        0x105a86ae4 closure #1 in ObservableType.subscribe(onNext:onError:onCompleted:onDisposed:) + 65 (ObservableType+Extensions.swift:65)
11 RxSwift                        0x105a86ce4 partial apply for closure #1 in ObservableType.subscribe(onNext:onError:onCompleted:onDisposed:) (<compiler-generated>)
12 RxSwift                        0x105a395fc AnonymousObserver.onCore(_:) + 22 (AnonymousObserver.swift:22)
13 RxSwift                        0x105a89540 ObserverBase.on(_:) + 16 (ObserverBase.swift:16)
14 RxSwift                        0x105a896fc protocol witness for ObserverType.on(_:) in conformance ObserverBase<A> (<compiler-generated>)
15 RxSwift                        0x105a89b0c partial apply (<compiler-generated>)
16 RxSwift                        0x105aa37bc thunk for @escaping @callee_guaranteed (@in_guaranteed Event<A>) -> () (<compiler-generated>)
17 RxSwift                        0x105a42b70 BehaviorSubject.on(_:) + 14 (Bag+Rx.swift:14)
18 RxSwift                        0x105a43b40 protocol witness for ObserverType.on(_:) in conformance BehaviorSubject<A> (<compiler-generated>)
19 RxSwift                        0x105a898a0 ObserverType.onNext(_:) + 30 (ObserverType.swift:30)
20 RxRelay                        0x105a201e0 BehaviorRelay.accept(_:) + 20 (BehaviorRelay.swift:20)
21 OjO                            0x1045c9b10 closure #2 in CreditCardViewModel.updateUser(ccAccount:) + 60 (CreditCardViewModel.swift:60)
22 RxSwift                        0x105a86aa8 closure #1 in ObservableType.subscribe(onNext:onError:onCompleted:onDisposed:) + 68 (ObservableType+Extensions.swift:68)
23 RxSwift                        0x105a86ce4 partial apply for closure #1 in ObservableType.subscribe(onNext:onError:onCompleted:onDisposed:) (<compiler-generated>)
24 RxSwift                        0x105a395fc AnonymousObserver.onCore(_:) + 22 (AnonymousObserver.swift:22)
25 RxSwift                        0x105a895ac ObserverBase.on(_:) + 23 (ObserverBase.swift:23)
26 RxSwift                        0x105a896fc protocol witness for ObserverType.on(_:) in conformance ObserverBase<A> (<compiler-generated>)
27 RxSwift                        0x105a73fec ErrorProducer.subscribe<A>(_:) + 30 (Error.swift:30)
28 RxSwift                        0x105a867dc ObservableType.subscribe(onNext:onError:onCompleted:onDisposed:) + 80 (ObservableType+Extensions.swift:80)
29 OjO                            0x1045c9910 CreditCardViewModel.updateUser(ccAccount:) + 55 (CreditCardViewModel.swift:55)
30 OjO                            0x1045c9f9c closure #1 in CreditCardViewModel.getToken(cardInfo:) + 78 (CreditCardViewModel.swift:78)
31 OjO                            0x1045ca030 thunk for @escaping @callee_guaranteed (@guaranteed CCCAccount?, @guaranteed Error?) -> () (<compiler-generated>)
32 CardConnectConsumerSDK         0x106167638 (Missing)
33 CardConnectConsumerSDK         0x106167e80 (Missing)
34 CFNetwork                      0x1a2a3c2cc CFNetServiceBrowserSearchForServices
35 CFNetwork                      0x1a2a4c800 _CFHTTPMessageSetResponseProxyURL
36 Foundation                     0x19fbe961c __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__
37 Foundation                     0x19faf33d8 -[NSBlockOperation main]
38 Foundation                     0x19fbeb8a4 __NSOPERATION_IS_INVOKING_MAIN__
39 Foundation                     0x19faf3070 -[NSOperation start]
40 Foundation                     0x19fbec29c __NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION__
41 Foundation                     0x19fbebd68 __NSOQSchedule_f
42 libdispatch.dylib              0x19f4a481c _dispatch_block_async_invoke2
43 libdispatch.dylib              0x19f4f2184 _dispatch_client_callout
44 libdispatch.dylib              0x19f4a41d0 _dispatch_main_queue_callback_4CF$VARIANT$mp
45 CoreFoundation                 0x19f7a23c4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
46 CoreFoundation                 0x19f79d3b8 __CFRunLoopRun
47 CoreFoundation                 0x19f79c8bc CFRunLoopRunSpecific
48 GraphicsServices               0x1a9608328 GSEventRunModal
49 UIKitCore                      0x1a38326d4 UIApplicationMain
50 OjO                            0x104583f64 main + 21 (MappableObject.swift:21)
51 libdyld.dylib                  0x19f627460 start

Replies

My best guess as to what’s happening here is as follows:

  1. In frame 8 your code has tried to get the

    localizedDescription
    property on the string.
  2. Frame 7 is that property’s getter. You can see it in in the Swift open source here:

    open var localizedDescription: String {
        if let localizedDescription = userInfo[NSLocalizedDescriptionKey] as? String {
            return localizedDescription
        } else {
            …
        }
    }

    Note This is from the top-of-tree. It’s hard to provide links to the exact version you’re running because you only posted an excerpt from your crash report. Still, this stuff hasn’t changed much recently.

    As you can see, its implementation is pretty obvious: It gets the

    NSLocalizedDescriptionKey
    property from the user info dictionary and tries to construct a string from that.
  3. Frames 6 and 5 are the code that constructs a Swift string from a bridged

    NSString
    . Again, you can see the code here. My best guess is that you’re running the
    .cocoa
    branch, resulting in a call to
    CFStringCreateCopy
    on line 386.
  4. This is where things start going wrong. Code at the CF level detects whether the passed in object is an Objective-C object and, if so, calls the relevant method on the object. In the specific case of

    CFStringCreateCopy
    , if the object looks like an Objective-C object it calls
    -copy
    on that.
  5. My guess is that this object isn’t a string, but rather an

    NSSet
    . The
    -[NSSet copy]
    method calls through to
    -[NSSet copyWithZone:]
    , and that method copies the set by calling
    -initWithSet:copyItems:
    . This is how you end up in that method in frame 2.

    Note There are some holes in this narrative, specifically because the method in frame 2 is an

    NSOrderedSet
    rather than
    NSSet
    . I haven’t quite been able to reconcile that, but I still think the overall narrative is useful.

Problems like this are usually caused by memory management bugs. For example, if the code that puts the string into the user info dictionary over-releases the string, that string can be released out from underneath the

NSError
. If that memory is then reused for an
NSSet
, you run into exactly this bug.

The first step in debugging problems like this is applying the standard memory debugging tools, and specifically zombies.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"