Crash inside of Vision framework during VNImageRequestHandler use

Hello,

I've been dealing with a puzzling issue for some time now, and I’m hoping someone here might have insights or suggestions.

The Problem: We’re observing an occasional crash in our app that seems to originate from the Vision framework.

  • Frequency: It happens randomly, after many successful executions of the same code, hard to tell how long the app was working, but in some cases app could run for like a month without any issues.
  • Devices: The issue doesn't seem device-dependent (we’ve seen it on various iPad models).
  • OS Versions: The crashes started occurring with iOS 18.0.1 and are still present in 18.1 and 18.1.1.
  • What I suspected: The crash logs point to a potential data race within the Vision framework.

The relevant section of the code where the crash happens:

guard let cgImage = image.cgImage else {
    throw ...
}
let request = VNCoreMLRequest(model: visionModel)

try VNImageRequestHandler(cgImage: cgImage).perform([request]) // <- the line causing the crash

Since the code is rather simple, I'm not sure what else there could be missing here.

  • The images sent here are uniform (fixed size).
  • Model is loaded and working, the crash occurs random after a period of time and the call worked correctly many times. Also, the model variable is not an optional.

Here is the crash log:

libobjc.A	objc_exception_throw
CoreFoundation	-[NSMutableArray removeObjectsAtIndexes:]
Vision	-[VNWeakTypeWrapperCollection _enumerateObjectsDroppingWeakZeroedObjects:usingBlock:]
Vision	-[VNWeakTypeWrapperCollection addObject:droppingWeakZeroedObjects:]
Vision	-[VNSession initWithCachingBehavior:]
Vision	-[VNCoreMLTransformer initWithOptions:model:error:]
Vision	-[VNCoreMLRequest internalPerformRevision:inContext:error:]
Vision	-[VNRequest performInContext:error:]
Vision	-[VNRequestPerformer _performOrderedRequests:inContext:error:]
Vision	-[VNRequestPerformer _performRequests:onBehalfOfRequest:inContext:error:]
Vision	-[VNImageRequestHandler performRequests:gatheredForensics:error:]
OurApp	ModelWrapper.perform

And I'm a bit lost at this point, I've tried everything I could image so far.

I've tried to putting a symbolic breakpoint in the removeObjectsAtIndexes to check if some library (e.g. crash reporter) we use didn't do some implementation swap. There was none, and if anything did some method swizzling, I'd expect that to show in the stack trace before the original code would be called. I did peek into the previous functions and I've noticed a lock used in one of the Vision methods, so in my understanding any data race in this code shouldn't be possible at all. I've also put breakpoints in the NSLock variants, to check for swizzling/override with a category and possibly messing the locking - again, nothing was there.

There is also another model that is running on a separate queue, but after seeing the line with the locking in the debugger, it doesn't seem to me like this could cause a problem, at least not in this specific spot.

Is there something I'm missing here, or something I'm doing wrong?

Thanks in advance for your help!

Oh, right, and sorry for forgetting to write the crucial part in the post - what's the crash.

It's an out of bounds: -[NSMutableArray removeObjectsAtIndexes:]: index 1 in index set beyond bounds [0 .. 0] where it would look like the array was already cleared

Crash inside of Vision framework during VNImageRequestHandler use
 
 
Q