Crash when iterating over CMSensorDataList via NSFastEnumeration

When iterating over a CMSensorDataList (which conforms to NSFastEnumeration) returned by CoreMotion's CMSensorRecorder's accelerometerData(from:to:) method, we sometimes see the crash below, with the frame above pointing to the close brace of the for loop iterating over the list.

0 CoreMotion 0x000000019fcf01dc CLInternalGetPinnedLocationAuthorizationState + 423316 1 CoreMotion 0x000000019fcf036b CLInternalGetPinnedLocationAuthorizationState + 423715 2 CoreMotion 0x000000019fcf02cb CLInternalGetPinnedLocationAuthorizationState + 423555 3 CoreMotion 0x000000019fbb8553 CLMotionActivity::isStatic() const + 1751823

It has been incredibly difficult to reproduce. We managed to reproduce a similar crash stack twice in a separate app that misused accelerometerData(from:to:) by providing a from and to Date that were more than 12 hours apart and iterating over the returned data, but it only occurred twice in many hours of testing. We hope to know if this could be a bug in Core Motion or it is definitely in our SDK, or if there is any way to fail gracefully in this scenario.

Thanks for the post, I’m not in that team, but I think that the team will look at your post and will ask you to submit a comprehensive crash report, adhering to the guidelines outlined in Posting a Crash Report.

https://developer.apple.com/forums/thread/688669

I think if you provide that now, will be easier for any knowledgeable developer of that domain to help you figure it out.

I believe CMSensorDataList is not a standard Array; it is an active iterator. When you call accelerometerData(from:to:), Core Motion doesn't immediately load all the data into memory. Instead, it sets up an XPC connection to locationd (which reads from a local SQLite database)?

When you iterate over it using a for...in loop in Swift, it uses NSFastEnumeration under the hood. This fetches data in batches. When you hit the closing brace } and the current batch is exhausted, Swift calls back into CoreMotion to fetch the next batch. This will be easy to reproduce with some code.

Are you request a massive time window (like > 12 hours), when asks for the next batch, the underlying iterator encounters a corrupted state or a dead XPC connection and hard-crashes (usually an EXC_BAD_ACCESS)?

I think you should provide at the request and how many items gets returned, you should post what’s your query and the time requested.

Albert Pascual
  Worldwide Developer Relations.

Crash when iterating over CMSensorDataList via NSFastEnumeration
 
 
Q