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?
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.
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.
Related
I just added AdMob into my UIViewController, and when I run the app, it freezes for a few seconds, and then this shows up:
=================================================================
Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 8093, TID: 1911410, Thread name: com.apple.CoreMotion.MotionThread, Queue name: com.apple.root.default-qos.overcommit, QoS: 0
Backtrace:
4 libobjc.A.dylib 0x0000000198a9b894 <redacted> + 56
5 CoreMotion 0x000000019f321040 CoreMotion + 307264
6 CoreMotion 0x000000019f321574 CoreMotion + 308596
7 CoreMotion 0x000000019f321484 CoreMotion + 308356
8 CoreMotion 0x000000019f352c64 CoreMotion + 511076
9 CoreMotion 0x000000019f352cc4 CoreMotion + 511172
10 CoreFoundation 0x00000001998324fc <redacted> + 28
11 CoreFoundation 0x0000000199831de0 <redacted> + 276
12 CoreFoundation 0x000000019982d0e4 <redacted> + 2324
13 CoreFoundation 0x000000019982c4b8 CFRunLoopRunSpecific + 452
14 CoreFoundation 0x000000019982d21c CFRunLoopRun + 84
15 CoreMotion 0x000000019f3525fc CoreMotion + 509436
16 libsystem_pthread.dylib 0x00000001994a7974 <redacted> + 132
17 libsystem_pthread.dylib 0x00000001994a78d0 _pthread_start + 52
18 libsystem_pthread.dylib 0x00000001994afddc thread_start + 4
Any tips?
Your problem is this process is in the Main Thread . Main Thread for only UI action. Otherwise UI will freeze.
You should do it in background thread.
DispatchQueue.global(qos: .background).async {
// Your code
}
Enjoy.
After upgrading to XCode 9 I suddently get the following warning when I run my app:
Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 15473, TID: 773864, Thread name: com.google.Maps.LabelingBehavior, Queue name: com.apple.root.default-qos.overcommit, QoS: 21
Backtrace:
4 App 0x000000010b8ee524 GMSIsApplicationInBackground + 53
5 App 0x000000010b8dc77a -[GMSForegroundDispatchQueue initWithName:targetQueue:] + 269
6 App 0x000000010b9bc5ee _ZN7gmscore6vector4text8GlyphSetC2ERKNS_4base10reffed_ptrINS0_16TextureAtlasPoolEEEPU28objcproto17OS_dispatch_queue8NSObjectPK8__CTFontff + 344
7 App 0x000000010b9bba58 _ZN7gmscore6vector4text10GlyphCache11GetGlyphSetEPK8__CTFontf + 214
8 App 0x000000010b9b950e _ZN7gmscore6vector4text6GLText14PrefetchGlyphsERKNS_4base10reffed_ptrINS1_10GlyphCacheEEERKNSt3__16vectorItNS9_9allocatorItEEEEPK8__CTFontf + 22
9 App 0x000000010b9b9611 _ZN7gmscore6vector4text6GLText14PrefetchGlyphsERKNS_4base10reffed_ptrINS1_10GlyphCacheEEEPK8__CTLinebf + 207
10 App 0x000000010b9112df _ZN7gmscore6vector12GLPointLabel22PrefetchGlyphsForGroupEPNS0_12GLLabelGroupE + 181
11 App 0x000000010b911207 _ZN7gmscore6vector12GLPointLabel14PrefetchGlyphsEv + 33
12 App 0x000000010b98022a _ZN7gmscore6vector16LabelingBehavior23CreatePendingOperationsERKNSt3__13setINS_4base10reffed_ptrINS0_7GLLabelEEENS2_4lessIS7_EENS2_9allocatorIS7_EEEESE_SE_NS0_13LabelDrawModeE + 1096
13 App 0x000000010b97fb9d _ZN7gmscore6vector16LabelingBehavior14RunLabelingJobERKNS_4base10reffed_ptrINS1_11LabelingJobEEE + 357
14 App 0x000000010b97fa2a ___ZN7gmscore6vector16LabelingBehavior14CommitInternalEPNS_8renderer14EntityRendererE_block_invoke + 22
15 Foundation 0x0000000110bb0948 __NSThreadPerformPerform + 334
16 CoreFoundation 0x00000001117af2b1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
17 CoreFoundation 0x000000011184ed31 __CFRunLoopDoSource0 + 81
18 CoreFoundation 0x0000000111793c19 __CFRunLoopDoSources0 + 185
19 CoreFoundation 0x00000001117931ff __CFRunLoopRun + 1279
20 CoreFoundation 0x0000000111792a89 CFRunLoopRunSpecific + 409
21 Foundation 0x0000000110b6ae5e -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 274
22 App 0x000000010b9bace5 -[GMSx_GTMSimpleWorkerThread main] + 337
23 Foundation 0x0000000110b788ac __NSThread__start__ + 1197
24 libsystem_pthread.dylib 0x000000011323393b _pthread_body + 180
25 libsystem_pthread.dylib 0x0000000113233887 _pthread_body + 0
26 libsystem_pthread.dylib 0x000000011323308d thread_start + 13
Error Domain=kCLErrorDomain Code=0 "(null)"
I can narrow it down to the problem happens when I assign the map to my view.
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.isIdleTimerDisabled = true
GPSManager.instance.startGPS()
setupLayout()
map = GMSMapView.map(withFrame: CGRect.zero, camera: getCamera(location: initialLocation, zoom: 10))
map.mapType = .hybrid
map.delegate = self
map.settings.consumesGesturesInView = false
self.view.addSubview(map) <-- This is where is goes wrong
self.view.sendSubview(toBack: map)
configureMenus()
}
Is this something I can fix or is it inside Google Maps? Cheers
The problem is inside the Google library.
If you don't want this kind of warning you can disable Main Thread Checker inside the Scheme options.
Go Product > Scheme > Manage Schemes search the Scheme you use for your tests and press Edit... in the left sidebar press Run and then go to Diagnostics and uncheck Main Thread Checker checkbox.
This is only a temporal solution if you don't want these warnings. I do not recommend it because you could introduce bugs if call UI inside a background thread
If you have test you should disable the checkbox in Test option as I explained in Xcode 9, iOS 11, XCUITest failure: Main Thread Checker Flurry Analytics if you do not want the tests crash
I started getting this warning :
This application is modifying the autolayout engine from a background
thread, which can lead to engine corruption and weird crashes. This
will cause an exception in a future release.
Here is the backtrace with the warning message :
Stack:(
0 CoreFoundation 0x25542303 <redacted> + 150
1 libobjc.A.dylib 0x24d0edff objc_exception_throw + 38
2 CoreFoundation 0x25542231 <redacted> + 0
3 Foundation 0x25e25bbb <redacted> + 170
4 Foundation 0x25ccb637 <redacted> + 38
5 UIKit 0x297e0431 <redacted> + 52
6 UIKit 0x297e0e1f <redacted> + 222
7 UIKit 0x297fd52d <redacted> + 96
8 UIKit 0x29efe579 <redacted> + 320
9 UIKit 0x299dc8e9 <redacted> + 148
10 UIKit 0x299cb44f <redacted> + 42
11 UIKit 0x296d5a83 <redacted> + 714
12 QuartzCore 0x277b1ad5 <redacted> + 128
13 QuartzCore 0x277ad1d1 <redacted> + 352
14 QuartzCore 0x277ad061 <redacted> + 16
15 QuartzCore 0x277ac581 <redacted> + 368
16 QuartzCore 0x277ac233 <redacted> + 614
17 QuartzCore 0x277d9b63 <redacted> + 310
18 libsystem_pthread.dylib 0x25279905 <redacted> + 508
19 libsystem_pthread.dylib 0x25279507 <redacted> + 86
20 libsystem_pthread.dylib 0x2527a485 pthread_exit + 28
21 Foundation 0x25ca31d7 <redacted> + 10
22 Foundation 0x25d5e34f <redacted> + 1178
23 libsystem_pthread.dylib 0x2527ac7f <redacted> + 138
24 libsystem_pthread.dylib 0x2527abf3 _pthread_start + 110
25 libsystem_pthread.dylib 0x25278a08 thread_start + 8
As far as i know i don't use Autolayout (as i want the app to be iOS 5.1.1 compatible). and also i don't seem to be within the reported backtrace.
I also have the PSPDFUIKitMainThreadGuard class enabled, which should be checking the main thread accesses but nothing trigered out of this thing.
Is there any way how to find out what is doing such a thing?
Question This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes doesn't explain how to debug such thing.
Whether or not you are using Autolayout directly is not the issue at hand. The OS is converting legacy autoresizingMask to modern NSLayoutConstraint under the hood.
The issue has to do with modifying the UI from anywhere but the main tread.
This typically happens when:
responding to a NSNotification
returning from an async method
executing a completion block on a thread you do not control
explicitly scheduling a task in the background
Note about The app doesn't crash...
The app will crash. All you need is time and a few thousand users...
I've got a crash that, according to the crash report, isn't my fault. I wish that were true. I've Profiled the app with NSZombies and it hasn't happened there. Here are excerpts from the crash report:
Date/Time: 2015-11-23 19:40:34.34 -0600
Launch Time: 2015-11-23 18:49:43.43 -0600
OS Version: iOS 9.1 (13B143)
Report Version: 104
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000c
Triggered by Thread: 7
Thread 7 Crashed:
0 libobjc.A.dylib 0x34fcbac6 objc_msgSend + 6
1 UIFoundation 0x31e7242e +[NSStringDrawingTextStorageSettings threadSpecificStringDrawingTextStorageSettings:] + 62
2 UIFoundation 0x31e675de +[NSString(NSStringDrawing) typesetterBehavior] + 34
3 UIFoundation 0x31e68ae2 __NSStringDrawingEngine + 298
4 UIFoundation 0x31e68908 -[NSString(NSExtendedStringDrawing) drawWithRect:options:attributes:context:] + 144
5 UIKit 0x276aa488 -[UILabel _drawTextInRect:baselineCalculationOnly:] + 4864
6 UIKit 0x2771b40c -[UILabel drawTextInRect:] + 540
7 UIKit 0x2771b1e4 -[UILabel drawRect:] + 88
8 UIKit 0x2771b15e -[UIView(CALayerDelegate) drawLayer:inContext:] + 386
9 QuartzCore 0x26f8b6fc -[CALayer drawInContext:] + 228
10 QuartzCore 0x26f75088 CABackingStoreUpdate_ + 1852
11 QuartzCore 0x270619d0 ___ZN2CA5Layer8display_Ev_block_invoke + 52
12 QuartzCore 0x26f745c8 CA::Layer::display_() + 1168
13 QuartzCore 0x26f588a0 CA::Layer::display_if_needed(CA::Transaction*) + 204
14 QuartzCore 0x26f58560 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 24
15 QuartzCore 0x26f57a78 CA::Context::commit_transaction(CA::Transaction*) + 368
16 QuartzCore 0x26f5772a CA::Transaction::commit() + 614
17 QuartzCore 0x26f84ed2 CA::Transaction::release_thread(void*) + 310
18 libsystem_pthread.dylib 0x3589e54c _pthread_tsd_cleanup + 508
19 libsystem_pthread.dylib 0x3589e14e _pthread_exit + 86
20 libsystem_pthread.dylib 0x3589db3c _pthread_wqthread + 1044
21 libsystem_pthread.dylib 0x3589d718 start_wqthread + 8
My application isn't listed in here anywhere within the crashed thread. I have a few questions:
How do I interpret this report to find the trigger of the crash? I see UILabels referenced in there, and I have plenty of those, how would I know which might be the cause?
Is the crash one of the lines in the middle and the report is showing actions before and after to provide context?
Thank you for your help. This debugging is confusing stuff.
Update: 11/27/15:
Do I need to make sure a declaration like the following is done on the main thread:
UIView *lineView;
or only the modifications like the following:
lineView = [[UIView alloc] initWithFrame:CGRectMake(15, cell.contentView.frame.size.height - 1.0, cell.contentView.frame.size.width, 1)];
I'm having trouble determining what needs to be wrapped in GCD dispatches and what can be done on a background thread. I've been making changes per the suggested answer but I'm still getting the same crash.
Thanks.
You are crashing because you violated the rule that UIView descendants can only be manipulated on the main thread. Crashing isn't necessarily the only outcome, but in Apple's view, it's perfectly acceptable.
Fix your code so that it dispatches that bit of work to the main thread.
I am currently developing an app in Spritekit with Swift. The app worked perfectly on device and in the simulator until I updated to Xcode 6 Beata 3 and iOS 8 Beta 3. When I run the app in the simulator everything still works perfect. But running the app on the device causes a exc_bad_acces when I create a SKTextureAtlas. Is anyone facing a similar problem or can anyone help me how to solve this.
I've alredy tried to run it on a device with iOS8 Beta 2 but I run into the same error here. I also deleted the app but that also didn't solve my problem when running the app on a device.
Here is the place it crashes:
var atlas = SKTextureAtlas(named: "ImageSet")
and that is the log from the device:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0xfffffffc
Triggered by Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libswift_stdlib_core.dylib 0x003a737e 0x286000 + 1184638
1 My Game 0x000f6900 Swift._doesImplicitlyUnwrappedOptionalHaveValue <A>(#inout A!) -> Builtin.Int1 (ComputerPlayer.swift:0)
2 My Game 0x0011c0a0 #!objc ObjectiveC.SKTextureAtlas.__allocating_init (ObjectiveC.SKTextureAtlas.Type)(named : Swift.String!) -> ObjectiveC.SKTextureAtlas (SpriteKit-1CMA4777KYE20.pcm:1)
3 My Game 0x0010e010 My_Game.GameScene.didMoveToView (My_Game.GameScene)(ObjectiveC.SKView) -> () (GameScene.swift:132)
4 My Game 0x00113218 #objc My_Game.GameScene.didMoveToView (My_Game.GameScene)(ObjectiveC.SKView) -> () (GameScene.swift:0)
5 SpriteKit 0x2e6d6340 SKGenerateNormalMap + 65680
6 SpriteKit 0x2e6f02de SKGenerateNormalMap + 172078
7 My Game 0x0011fd38 My_Game.GameViewController.viewWillLayoutSubviews (My_Game.GameViewController)() -> () (GameViewController.swift:71)
8 My Game 0x00121468 #objc My_Game.GameViewController.viewWillLayoutSubviews (My_Game.GameViewController)() -> () (GameViewController.swift:0)
9 UIKit 0x2e844266 0x2e837000 + 53862
10 QuartzCore 0x2e2804c0 CABackingStoreGetTypeID + 892
11 QuartzCore 0x2e27bea0 CARenderServerGetServerPort + 5364
12 QuartzCore 0x2e27bd28 CARenderServerGetServerPort + 4988
13 QuartzCore 0x2e27b70c CARenderServerGetServerPort + 3424
14 QuartzCore 0x2e27b510 CARenderServerGetServerPort + 2916
15 UIKit 0x2eaa6cf8 _UIApplicationIsExtension + 4936
16 UIKit 0x2eaa79e0 _UIApplicationIsExtension + 8240
17 UIKit 0x2eab1a68 _UIApplicationUsesAlternateUI + 38388
18 UIKit 0x2eaa5ec8 _UIApplicationIsExtension + 1304
19 FrontBoardServices 0x319bd04e _FBSLog + 2198
20 CoreFoundation 0x2b1692d2 CFRunLoopTimerSetTolerance + 5878
21 CoreFoundation 0x2b168594 CFRunLoopTimerSetTolerance + 2488
22 CoreFoundation 0x2b166d16 _CFRunLoopGet2b + 2174
23 CoreFoundation 0x2b0b624c CFRunLoopRunSpecific + 472
24 CoreFoundation 0x2b0b605e CFRunLoopRunInMode + 102
25 UIKit 0x2e8ab5ba _UISharedImageSetLoadFactor + 3906
26 UIKit 0x2e8a6844 UIApplicationMain + 1436
27 My Game 0x0012a940 top_level_code (AppDelegate.swift:0)
28 My Game 0x0012a97c main (AppDelegate.swift:0)
29 libdyld.dylib 0x385aaaac _tlv_atexit + 8
Thank you
Try running Clean on the project, a few people had problems with bad access, me included, when updating. Some resolved it with clean and some withe a fresh copy of the project.