How Can I Solve An EXC_BAD_ACCESS That Occurs Within the CoreMotion of IOS

I need help solving a problem where I experience an occasional EXC_BAD_ACCESS crash that occurs in the CoreMotion. Most of them happened in foreground and it's not easy to reproduce the bug.


The following is the call trace:

Thread : Crashed: Thread
0  libobjc.A.dylib                0x19618bbd0 objc_msgSend + 16
1  CoreMotion                     0x184d90538 (null)
2  CoreFoundation                 0x18445bfc8 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
3  CoreFoundation                 0x18445b0d0 __CFRunLoopDoBlocks + 312
4  CoreFoundation                 0x1844592d0 __CFRunLoopRun + 696
5  CoreFoundation                 0x184384f74 CFRunLoopRunSpecific + 396
6  CoreFoundation                 0x1843d6ffc CFRunLoopRun + 112
7  CoreMotion                     0x184d90234 (null)
8  libsystem_pthread.dylib        0x1969d7db8 _pthread_body + 164
9  libsystem_pthread.dylib        0x1969d7d14 _pthread_body


After analysing I found that some private apis called before the crash.


2 CoreMotion 0x1851285dc CLMotionCore::executeBlockOnMotionThread(void () block_pointer) + 156
3 CoreMotion 0x1851144e8 -[CMMotionManager willResignActive:] + 76


Does anybody have an idea of what could possibly be happening or what should I do to solve it? Thanks!

The default assumption for this sort of crash is a memory management error.


Notice that the crash occurred in objc_msgSend, which is the central runtime method for dispatching method invocations to Obj-C objects. You'd get a different error if the message was unknown to the object (an Obj-C exception rather than crash), so the most likely cause is that the object pointer is invalid, because the object has been released, and probably reused for something else.


Because of the helpful __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ symbol further up the backtrace, you know that the thread's run loop is trying to invoke a block of code (a closure, it's called in Swift). Most likely, then, one of the variables captured by the block wasn't retained, and therefore has been deallocated. In particular, this could be a block you've arranged to capture 'self' weakly, to prevent a reference cycle.


Of course, all of that could be happening in the Cocoa frameworks, as a result of some other object being deallocated, so it's not easy to know what's going on. However, you also assume that the error is ultimately in your code, not in Cocoa frameworks.


One way to look for a solution is to turn "zombies" on, in the scheme you use to run the app from Xcode. This should crash earlier, if you're really trying to use a deallocated object somewhere.


The other thing about the backtrace is that its structure suggests this is a background thread. The other possible error is if you use any Cocoa APIs in that thread that are restricted to the main thread. However, this smells more like a memory management problem than a thread abuse problem.


Is that any help?

Thanks for your detailed reply. Now the difficult thing is that it's not easy to reproduce it. I will take your advise and review my code.

How Can I Solve An EXC_BAD_ACCESS That Occurs Within the CoreMotion of IOS
 
 
Q