crash when modelWithContentsOfURL in iOS 16+

We have a code that crashed The crash stack is as follows

Thread 26 Crashed:
0   CoreFoundation                  0x0000000198b0569c CFRelease + 44
1   CoreFoundation                  0x0000000198b12334 __CFBasicHashRehash + 1172
2   CoreFoundation                  0x0000000198b015dc __CFBasicHashAddValue + 100
3   CoreFoundation                  0x0000000198b232e4 CFDictionarySetValue + 208
4   Foundation                      0x00000001979b0378 _getStringAtMarker + 464
5   Foundation                      0x00000001979b016c _NSXPCSerializationStringForObject + 56
6   Foundation                      0x00000001979cec4c __44-[NSXPCDecoder _decodeArrayOfObjectsForKey:]_block_invoke + 52
7   Foundation                      0x00000001979ceb90 _NSXPCSerializationIterateArrayObject + 208
8   Foundation                      0x00000001979cda7c -[NSXPCDecoder _decodeArrayOfObjectsForKey:] + 240
9   Foundation                      0x00000001979cd1bc -[NSDictionary(NSDictionary) initWithCoder:] + 176
10  Foundation                      0x00000001979ae6e8 _decodeObject + 1264
11  Foundation                      0x00000001979cec4c __44-[NSXPCDecoder _decodeArrayOfObjectsForKey:]_block_invoke + 52
12  Foundation                      0x00000001979ceb90 _NSXPCSerializationIterateArrayObject + 208
13  Foundation                      0x00000001979cda7c -[NSXPCDecoder _decodeArrayOfObjectsForKey:] + 240
14  Foundation                      0x00000001979cd1a4 -[NSDictionary(NSDictionary) initWithCoder:] + 152
15  Foundation                      0x00000001979ae6e8 _decodeObject + 1264
16  Foundation                      0x00000001979ad030 -[NSXPCDecoder _decodeObjectOfClasses:atObject:] + 148
17  Foundation                      0x0000000197a0a7f0 _NSXPCSerializationDecodeTypedObjCValuesFromArray + 892
18  Foundation                      0x0000000197a0a1f8 _NSXPCSerializationDecodeInvocationArgumentArray + 412
19  Foundation                      0x0000000197a0866c -[NSXPCDecoder __decodeXPCObject:allowingSimpleMessageSend:outInvocation:outArguments:outArgumentsMaxCount:outMethodSignature:outSelector:isReply:replySelector:] + 700
20  Foundation                      0x0000000197a61078 -[NSXPCDecoder _decodeReplyFromXPCObject:forSelector:] + 76
21  Foundation                      0x0000000197a5f690 -[NSXPCConnection _decodeAndInvokeReplyBlockWithEvent:sequence:replyInfo:] + 252
22  Foundation                      0x0000000197a63664 __88-[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:]_block_invoke_5 + 188
23  Foundation                      0x0000000197a08058 -[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:] + 2244
24  CoreFoundation                  0x0000000198b19d88 ___forwarding___ + 1016
25  CoreFoundation                  0x0000000198b198d0 _CF_forwarding_prep_0 + 96
26  AppleNeuralEngine               0x00000001e912ab1c -[_ANEDaemonConnection loadModel:sandboxExtension:options:qos:withReply:] + 332
27  AppleNeuralEngine               0x00000001e912a674 __44-[_ANEClient doLoadModel:options:qos:error:]_block_invoke + 360
28  libdispatch.dylib               0x00000001a0a21dd4 _dispatch_client_callout + 20
29  libdispatch.dylib               0x00000001a0a312c4 _dispatch_lane_barrier_sync_invoke_and_complete + 56
30  AppleNeuralEngine               0x00000001e9129ef0 -[_ANEClient doLoadModel:options:qos:error:] + 500
31  Espresso                        0x00000001a7e02034 Espresso::ANERuntimeEngine::compiler::build_segment(std::__1::shared_ptr<Espresso::abstract_batch> const&, int, Espresso::net_compiler_segment_based::segment_t const&) + 3736
32  Espresso                        0x00000001a7e010cc Espresso::net_compiler_segment_based::build(std::__1::shared_ptr<Espresso::abstract_batch> const&, int, int) + 384
33  Espresso                        0x00000001a7df02a4 Espresso::ANERuntimeEngine::compiler::build(std::__1::shared_ptr<Espresso::abstract_batch> const&, int, int) + 120
34  Espresso                        0x00000001a7e1b3a4 Espresso::net::__build(std::__1::shared_ptr<Espresso::abstract_batch> const&, int, int) + 360
35  Espresso                        0x00000001a7e178e0 Espresso::abstract_context::compute_batch_sync(void (std::__1::shared_ptr<Espresso::abstract_batch> const&) block_pointer) + 112
36  Espresso                        0x00000001a7e198b8 EspressoLight::espresso_plan::prepare_compiler_if_needed() + 3208
37  Espresso                        0x00000001a7e183f4 EspressoLight::espresso_plan::prepare() + 1712
38  Espresso                        0x00000001a7da8e78 espresso_plan_build_with_options + 300
39  Espresso                        0x00000001a7da8d30 espresso_plan_build + 44
40  CoreML                          0x00000001b346645c -[MLNeuralNetworkEngine rebuildPlan:error:] + 536
41  CoreML                          0x00000001b3464294 -[MLNeuralNetworkEngine _setupContextAndPlanWithConfiguration:usingCPU:reshapeWithContainer:error:] + 3132
42  CoreML                          0x00000001b34797a0 -[MLNeuralNetworkEngine initWithContainer:configuration:error:] + 196
43  CoreML                          0x00000001b347962c +[MLNeuralNetworkEngine loadModelFromCompiledArchive:modelVersionInfo:compilerVersionInfo:configuration:error:] + 164
44  CoreML                          0x00000001b34792a0 +[MLLoader _loadModelWithClass:fromArchive:modelVersionInfo:compilerVersionInfo:configuration:error:] + 144
45  CoreML                          0x00000001b3478c64 +[MLLoader _loadModelFromArchive:configuration:modelVersion:compilerVersion:loaderEvent:useUpdatableModelLoaders:loadingClasses:error:] + 532
46  CoreML                          0x00000001b34650c8 +[MLLoader _loadWithModelLoaderFromArchive:configuration:loaderEvent:useUpdatableModelLoaders:error:] + 424
47  CoreML                          0x00000001b3474bc8 +[MLLoader _loadModelFromArchive:configuration:loaderEvent:useUpdatableModelLoaders:error:] + 460
48  CoreML                          0x00000001b347a024 +[MLLoader _loadModelFromAssetAtURL:configuration:loaderEvent:error:] + 244
49  CoreML                          0x00000001b3479cbc +[MLLoader loadModelFromAssetAtURL:configuration:error:] + 104
50  CoreML                          0x00000001b347ac2c -[MLModelAsset load:] + 564
51  CoreML                          0x00000001b347a9c4 -[MLModelAsset modelWithError:] + 24
52  CoreML                          0x00000001b347a7b4 +[MLModel modelWithContentsOfURL:configuration:error:] + 172
53  CoreML                          0x00000001b37afbc4 +[MLModel modelWithContentsOfURL:error:] + 76

Core code

MLModel* model = nil;
NSError *error = nil;
@try
{
    model = [MLModel modelWithContentsOfURL:modelURL error:&error];
}
@catch (NSException *exception)
{
    model = nil;
    return Ret_OperationErr_InvalidInit;
}

Two question:

  1. What does this stack mean?
  2. I added @ try @ catch, why is it still crashing?

Hello @GGGGGGGary, thank you for posting a stack trace. The crash doesn't seem to be related to an implementation issue, so please use Feedback Assistant to submit a bug report, and please post here your bug report's ID.

If the model is a fixed asset, you can simply drag it onto your Xcode project instead of calling modelWithContentsOfURL:error:. Please see Integrating a Core ML Model into Your App for a sample implementation.

If the model is a remote asset, you need to compile it before calling modelWithContentsOfURL:error:. Please refer to the Downloading and Compiling a Model on the User’s Device article for detailed information.

crash when modelWithContentsOfURL in iOS 16+
 
 
Q