CMotionManager crash on instantiation
Asked Answered
P

3

12

EDIT: This appears to be resolved on iOS 13. Leaving all the original details below.

I'm getting a crash when instantiating my CMMotionManager object for Core Motion. This is on an iPhone Xs running iOS 12.0.1.

I can reliably reproduce this with a single-view app, with the following view controller.

import UIKit
import CoreMotion

class ViewController: UIViewController {
    var motion: CMMotionManager?
    override func viewDidLoad() {
        super.viewDidLoad()

        // This causes a crash on iPhone Xs, iOS 12.0.1
        self.motion = CMMotionManager()
    }
}

Full sample project is at https://github.com/doctorcolinsmith/motiontestcrash/tree/master

When running the above, I am getting a fault on a thread with the following output in the debugger.

=================================================================
Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 3634, TID: 630341, Thread name: com.apple.CoreMotion.MotionThread, Queue name: com.apple.root.default-qos.overcommit, QoS: 0
Backtrace:
4   libobjc.A.dylib                     0x000000019b0d3894 <redacted> + 56
5   CoreMotion                          0x00000001a19337a4 CoreMotion + 305060
6   CoreMotion                          0x00000001a1933cd8 CoreMotion + 306392
7   CoreMotion                          0x00000001a1933be8 CoreMotion + 306152
8   CoreMotion                          0x00000001a19653cc CoreMotion + 508876
9   CoreMotion                          0x00000001a196542c CoreMotion + 508972
10  CoreFoundation                      0x000000019be6c888 <redacted> + 28
11  CoreFoundation                      0x000000019be6c16c <redacted> + 276
12  CoreFoundation                      0x000000019be67470 <redacted> + 2324
13  CoreFoundation                      0x000000019be66844 CFRunLoopRunSpecific + 452
14  CoreFoundation                      0x000000019be675a8 CFRunLoopRun + 84
15  CoreMotion                          0x00000001a1964d64 CoreMotion + 507236
16  libsystem_pthread.dylib             0x000000019bae1a04 <redacted> + 132
17  libsystem_pthread.dylib             0x000000019bae1960 _pthread_start + 52
18  libsystem_pthread.dylib             0x000000019bae9df4 thread_start + 4
2018-10-24 16:19:31.423680-0700 motiontest[3634:630341] [reports] Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 3634, TID: 630341, Thread name: com.apple.CoreMotion.MotionThread, Queue name: com.apple.root.default-qos.overcommit, QoS: 0
Backtrace:
4   libobjc.A.dylib                     0x000000019b0d3894 <redacted> + 56
5   CoreMotion                          0x00000001a19337a4 CoreMotion + 305060
6   CoreMotion                          0x00000001a1933cd8 CoreMotion + 306392
7   CoreMotion                          0x00000001a1933be8 CoreMotion + 306152
8   CoreMotion                          0x00000001a19653cc CoreMotion + 508876
9   CoreMotion                          0x00000001a196542c CoreMotion + 508972
10  CoreFoundation                      0x000000019be6c888 <redacted> + 28
11  CoreFoundation                      0x000000019be6c16c <redacted> + 276
12  CoreFoundation                      0x000000019be67470 <redacted> + 2324
13  CoreFoundation                      0x000000019be66844 CFRunLoopRunSpecific + 452
14  CoreFoundation                      0x000000019be675a8 CFRunLoopRun + 84
15  CoreMotion                          0x00000001a1964d64 CoreMotion + 507236
16  libsystem_pthread.dylib             0x000000019bae1a04 <redacted> + 132
17  libsystem_pthread.dylib             0x000000019bae1960 _pthread_start + 52
18  libsystem_pthread.dylib             0x000000019bae9df4 thread_start + 4
(lldb) 

Has anyone encountered this before or have an idea on how to solve the crash?

Pythagorean answered 24/10, 2018 at 23:27 Comment(11)
Do you mean you're unable to reproduce the issue on other devices? And are only able to reproduce on an XR?Warila
With help of others, I have that it is reproducible on iPhone X and Xs, but not on iPhone SE, 6S, 7, or iPad (2017). That could mean it is specific to devices with the A11 chip or newer.Pythagorean
For reference, I have found that the same error occurs on my iPhone Xs with the iOS 12.1 beta release.Pythagorean
This is specific to the X series of iPhones. I'm getting same error on Xs Max, and don't have any problems on any other devices. And it occurs even when instantiation of core motion is within DispatchQueue.main.async block. It looks like an apple bug.Surfactant
This bug is still present on iPhone XS iOS12.1.2 Beta releaseAmidase
Same issue on iOS 12.2 on iPhone XS, no warning on iPhone 8 also running iOS 12.2. Found 2 radar for this : openradar.appspot.com/46210367 and openradar.appspot.com/45003816, also a topic here : github.com/Instabug/Instabug-iOS/issues/298Arteriotomy
is it confirmed as a bug ? Im getting the same log on iOS 12 iPhone XS!!Lineberry
Yes, Apple seems to know about it at this point.Pythagorean
This is still on-going. Turns-out this exact same iPhoneXS problem occurs on the latest gen (2018 gen 3) iPads too.Surinam
I'm still getting this on iPhone XR running iOS 12.3.1 Behavior is odd. If I launch the app in debug mode, the stack track appears as though a crash occurred but the app continues running without any problem. However, if I run UI tests, this same CoreMotion event triggers the same stack trace and legitimately crashes the app, preventing tests from running.Minima
This appears to be fixed on iOS 13. Yay!Pythagorean
T
4

Well, I've tested your application both physical iPhone 6S and iPhone 7 - obviously with iOS 12.0.1. I've also checked for a couple of things in your project and couldn't reproduce your problem at all - not even in simulators - and I don't think it's possible to download a simulator with specific fixes (12.0.x), only with minor version updates - so 12.1 being the next one. This looks really specific on iPhone XS hardware, and I would suggest you to fill an issue with Apple support.

EDIT: Well, I saw that you've already filled a bug report on openradar. This guy even referenced the bug report status on openradar-mirror.

Tirrell answered 25/10, 2018 at 2:49 Comment(5)
Thanks for checking those two devices. I can also confirm an iPhone SE and iPad (2017 model) are both good. Whereas, iPhone X and Xs are definitely showing the issue. Incidentally, I did post a bug to Apple, but that open radar isn't mine, good to know other people are looking at this problem.Pythagorean
Yeah, but and to be honest, a REALLY odd error - considering the CoreMotion lib functionalities. Sorry that I can't help you further on this one buddy!Tirrell
I have the same issue in my project and it's only happening on my XS. iPhone 7 is fine. Really weird. Probably a bug.Erbil
Happens on Xs Max (but not my iPhone X) even when calling from main thread block. Doesn't happen on previous models.Surfactant
I'm on iOS 12.1.4 iPhone XS Max and this still does not work. I've filed a rdar on bugreport.apple.com and engineering has marked my bug as a duplicate. Lets hope they fix this soon.Den
B
-1

I don't know much about CoreMotion, but the error message tells you you're trying to call a UI action on a background thread. Try putting your code into:

DispatchQueue.main.async {
    //your code here
}

It also seems you're trying to do UI related actions in viewDidLoad(). Technically that's fine, but remember that some views probably aren't layed out at that point in time.

Balfour answered 25/10, 2018 at 0:6 Comment(2)
I wish this was as simple as a rogue UI call off the main thread :-P. The same error happens whether it's in a DispatchQueue.main.async block or not. The sample app has no other UI actions except for the viewDidLoad call on the view controller.Pythagorean
Clearly he is not doing any UI related actions in the viewDidLoad, only instantiating a CMMotionManager.Tirrell
B
-1

From this answer make sure you have Privacy - Motion Usage Description in your plist.info file.

Borrowing answered 22/6 at 13:49 Comment(1)
Irrelevant. That crash occurs when you start using the motion manager. This crash took place on instantiation, which is a completely different thing.Aliciaalick

© 2022 - 2024 — McMap. All rights reserved.