What needs to be executed on the main thread - ios

I've got a crash like the following:
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
I understand I need to execute the UIKit code on the main thread. However, I'm struggling with what exactly needs to be on the main thread. 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've been wrapping things in dispatch code and I'm still getting the same crash.
Thanks.

You should run on UI thread only code that shows something to user.
lineView = [[UIView alloc] initWithFrame:CGRectMake(15, cell.contentView.frame.size.height - 1.0, cell.contentView.frame.size.width, 1)];
this code you can run on background thread, but
[superview addSubview: lineView];
Should be executed on main thread.

Related

Crash AVFoundation in +[AVOutputContext(FigRoutingContext) currentRoutingContextFactory]

We have crash in AVFoundation.
AVFoundation
+[AVOutputContext(FigRoutingContext) currentRoutingContextFactory]
EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000a3c27beb8
Crashed: Thread
0 libobjc.A.dylib 0x183b8c910 objc_msgSend + 16
1 AVFoundation 0x18a5401bc +[AVOutputContext(FigRoutingContext) currentRoutingContextFactory] + 56
2 AVFoundation 0x18a53ae28 +[AVFigRoutingContextOutputContextImpl sharedAudioPresentationOutputContext] + 28
3 AVFoundation 0x18a4d0a24 +[AVOutputContext sharedAudioPresentationOutputContext] + 40
4 MediaPlayer 0x195deb1cc -[MPAVLightweightRoutingController pickedRoutes] + 68
5 MediaPlayer 0x195e04338 -[MPVolumeView _setShowsVolumeSlider:] + 96
6 MediaPlayer 0x195e02ad4 -[MPVolumeView layoutSubviews] + 208
7 UIKit 0x18e593770 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1420
8 QuartzCore 0x188b3525c -[CALayer layoutSublayers] + 184
9 QuartzCore 0x188b393ec CA::Layer::layout_if_needed(CA::Transaction*) + 324
10 QuartzCore 0x188aa5aa0 CA::Context::commit_transaction(CA::Transaction*) + 320
11 QuartzCore 0x188acd5d0 CA::Transaction::commit() + 580
12 QuartzCore 0x188acdd10 CA::Transaction::release_thread(void*) + 244
13 libsystem_pthread.dylib 0x1845e457c _pthread_tsd_cleanup + 572
14 libsystem_pthread.dylib 0x1845e42cc _pthread_exit + 88
15 libsystem_pthread.dylib 0x1845e40d4 _pthread_wqthread + 1472
16 libsystem_pthread.dylib 0x1845e3b08 start_wqthread + 4
The Crashlytics wrote:
This crash has non-main threads that appear to use UIKit classes or
functions. Generally speaking, it is only safe to use UIKit from the
main thread. Calling into UIKit from background threads can cause
unpredictable behavior (like crashes!). Please double check that all
of your code (that is not on the main thread) has been explicitly
marked as safe to use in the background by Apple.
But we don't use other threads in app.
Crashlytics logs:
Crashed: Thread 0 libobjc.A.dylib 0x184370910
objc_msgSend + 16 1 AVFoundation 0x18ad241bc
+[AVOutputContext(FigRoutingContext) currentRoutingContextFactory] + 56 2 AVFoundation 0x18ad1ee28
+[AVFigRoutingContextOutputContextImpl sharedAudioPresentationOutputContext] + 28 3 AVFoundation
0x18acb4a24 +[AVOutputContext sharedAudioPresentationOutputContext] +
40 4 MediaPlayer 0x1965cf1cc
-[MPAVLightweightRoutingController pickedRoutes] + 68 5 MediaPlayer 0x1965e8338 -[MPVolumeView _setShowsVolumeSlider:] + 96 6 MediaPlayer
0x1965e6ad4 -[MPVolumeView layoutSubviews] + 208 7 UIKit
0x18ed77770 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1420
8 QuartzCore 0x18931925c -[CALayer
layoutSublayers] + 184 9 QuartzCore 0x18931d3ec
CA::Layer::layout_if_needed(CA::Transaction*) + 324 10 QuartzCore
0x189289aa0 CA::Context::commit_transaction(CA::Transaction*) + 320 11
QuartzCore 0x1892b15d0 CA::Transaction::commit() +
580 12 QuartzCore 0x1892b1d10
CA::Transaction::release_thread(void*) + 244 13
libsystem_pthread.dylib 0x184dc857c _pthread_tsd_cleanup + 572
14 libsystem_pthread.dylib 0x184dc82cc _pthread_exit + 88 15
libsystem_pthread.dylib 0x184dc80d4 _pthread_wqthread + 1472 16
libsystem_pthread.dylib 0x184dc7b08 start_wqthread + 4
Init code:
self.volumeView = MPVolumeView.init(frame: CGRect.init(x: 0, y: 2,
width: 52.0,
height: 30.0))
self.volumeView?.showsVolumeSlider = false self.volumeView?.showsRouteButton = true
self.volumeView?.setRouteButtonImage(UIImage.init(named: "streaming_icon"), for: .normal)
self.volumeView?.setRouteButtonImage(UIImage.init(named: "selected_streaming_icon"), for: .selected)
self.volumeView.sizeToFit()
Please help resolve this issue.
I have contacted Apple and they have verified my theory that MPVolumeView performs UI layout changes on a background thread.
Bug report created.
Update 2018-05-30:
Engineering has determined that your bug report (40594280) is a duplicate of 39309134 and will be closed.
I have the same crash condition using MPVolumeView and AVPlayer.
With other weird things going on, like the volume slider disappearing.
The issues go away if I set the AVPlayer object attribute 'allowsExternalPlayback' to false.
player.allowsExternalPlayback = false
Seems strange but it worked.

Frequent WebThread Crash on iOS 11

Does anynone know how to fix this issue? There are so many crashes posted to Fabric, which only appeared on iOS 11. Fabric gives a tip:
This crash has non-main threads that appear to use UIKit classes or functions. Generally speaking, it is only safe to use UIKit from the main thread. Calling into UIKit from background threads can cause unpredictable behavior (like crashes!). Please double check that all of your code (that is not on the main thread) has been explicitly marked as safe to use in the background by Apple.
Below is the crash session:
Crashed: WebThread
0 libobjc.A.dylib 0x1845041a0 objc_retain + 16
1 UIKit 0x18e850a10 -[UIView(Hierarchy) subviews] + 76
2 UIKit 0x18eb4416c -[UIView(CALayerDelegate) _wantsReapplicationOfAutoLayoutWithLayoutDirtyOnEntry:] + 72
3 UIKit 0x18e856e60 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1348
4 QuartzCore 0x1892e3948 -[CALayer layoutSublayers] + 184
5 QuartzCore 0x1892e7ad0 CA::Layer::layout_if_needed(CA::Transaction*) + 332
6 QuartzCore 0x18925431c CA::Context::commit_transaction(CA::Transaction*) + 336
7 QuartzCore 0x18927bb40 CA::Transaction::commit() + 540
8 QuartzCore 0x18927c980 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 92
9 CoreFoundation 0x185246cdc __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
10 CoreFoundation 0x185244694 __CFRunLoopDoObservers + 412
11 CoreFoundation 0x185164c78 CFRunLoopRunSpecific + 468
12 WebCore 0x18d0d984c RunWebThread(void*) + 560
13 libsystem_pthread.dylib 0x184ec82b4 _pthread_body + 308
14 libsystem_pthread.dylib 0x184ec8180 _pthread_body + 310
15 libsystem_pthread.dylib 0x184ec6b74 thread_start + 4

IOS fullscreen video player crash issue

When I was debugging the iOS application, meet with some issue causing application to crash when playing video in fullscreen mode and this issue is concerned with the specific device iPhone 6s. The application works good in iPhone 6 and iPhone 5s without crash.
Code Type: ARM-64 (Native)
Parent Process: launchd [1]
Date/Time: 2016-09-12 17:41:50.50 -0700
Launch Time: 2016-09-12 17:41:27.27 -0700
OS Version: iOS 9.3.4 (13G35)
Report Version: 105
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x0000000180a96234
Triggered by Thread: 0
Filtered syslog:
None found
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 CoreFoundation 0x0000000180a96234 CFEqual + 512
1 CoreGraphics 0x0000000182025a3c CGGradientCreateWithColors + 232
2 CoreGraphics 0x0000000182025a3c CGGradientCreateWithColors + 232
3 UIKit 0x000000018621cf34 +[UIProgressView _tintedImageForHeight:andColors:roundingRectCorners:] + 356
4 UIKit 0x0000000186109524 __50-[UISlider _updateMaxTrackColorForInitialization:]_block_invoke + 148
5 UIKit 0x00000001860217b0 +[UIView _performSystemAppearanceModifications:] + 68
6 UIKit 0x000000018610942c -[UISlider _updateMaxTrackColorForInitialization:] + 476
7 UIKit 0x0000000185f74c08 -[UISlider _initImages] + 568
8 UIKit 0x0000000185f7495c -[UISlider layoutSubviews] + 92
9 MediaPlayer 0x0000000189ad0544 -[MPVolumeSlider layoutSubviews] + 56
10 UIKit 0x0000000185d141e4 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 656
11 QuartzCore 0x00000001836a698c -[CALayer layoutSublayers] + 148
12 QuartzCore 0x00000001836a15c8 CA::Layer::layout_if_needed(CA::Transaction*) + 292
13 UIKit 0x0000000185d2b0a4 -[UIView(Hierarchy) layoutBelowIfNeeded] + 924
14 UIKit 0x0000000185d30bec -[UISlider setValue:animated:] + 184
15 MediaPlayer 0x0000000189ad0c8c -[MPVolumeSlider volumeController:volumeValueDidChange:] + 84
16 MediaPlayer 0x0000000189b68074 __39-[MPVolumeController updateVolumeValue]_block_invoke_2 + 240
17 libdispatch.dylib 0x00000001806054bc _dispatch_call_block_and_release + 24
18 libdispatch.dylib 0x000000018060547c _dispatch_client_callout + 16
19 libdispatch.dylib 0x000000018060ab84 _dispatch_main_queue_callback_4CF + 1844
20 CoreFoundation 0x0000000180b70d50 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
21 CoreFoundation 0x0000000180b6ebb8 __CFRunLoopRun + 1628
22 CoreFoundation 0x0000000180a98c50 CFRunLoopRunSpecific + 384
23 GraphicsServices 0x0000000182380088 GSEventRunModal + 180
24 UIKit 0x0000000185d82088 UIApplicationMain + 204

My Application not Listed in Crashed Thread

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.

iPhone crash during layer animation after being in background

I'm having trouble fixing a pesky crash. I can't figure out how to accurately reproduce it,
but it only occurs after the app comes out of the background. I get this crash every 2+ days.
In applicationDidBecomeActive:, I restart a timer that fires every 0.4 seconds. It calls:
- (void)rotate {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
self.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-[LocationModel instance].heading.magneticHeading));
[UIView commitAnimations];
}
Also in RotateView, is the drawRect, whose body is at line 41 as seen in the crash report.
- (void)drawRect:(CGRect)rect {
[img drawInRect:rect]; // line 41
}
img is a small UIImage loaded with imageNamed: and stored in an ivar.
Any advice? I have little experience with crash reports.
Thanks!
Crash report:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x3001b482
Crashed Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libobjc.A.dylib 0x333a6c98 objc_msgSend + 16
1 MyApp 0x000259bc -[RotateView drawRect:] (RotateView.m:41)
2 UIKit 0x34f4a9fe -[UIView(CALayerDelegate) drawLayer:inContext:] + 258
3 QuartzCore 0x3058efa6 -[CALayer drawInContext:] + 86
4 QuartzCore 0x3058ed24 _ZL16backing_callbackP9CGContextPv + 32
5 QuartzCore 0x3058e776 CABackingStoreUpdate + 1226
6 QuartzCore 0x3058e178 -[CALayer _display] + 724
7 QuartzCore 0x3058de86 -[CALayer display] + 134
8 QuartzCore 0x30582706 CALayerDisplayIfNeeded + 178
9 QuartzCore 0x305821c6 CA::Context::commit_transaction(CA::Transaction*) + 214
10 QuartzCore 0x30581fd0 CA::Transaction::commit() + 184
11 QuartzCore 0x30580900 CA::Transaction::pop() + 120
12 QuartzCore 0x3058087e +[CATransaction commit] + 22
13 UIKit 0x35096bcc _UIWindowUpdateVisibleContextOrder + 136
14 UIKit 0x35096c60 +[UIWindow _prepareWindowsForAppResume] + 4
15 UIKit 0x3508f3de -[UIApplication _handleApplicationResumeEvent:] + 66
16 UIKit 0x34f39e20 -[UIApplication handleEvent:withNewEvent:] + 2724
17 UIKit 0x34f3920e -[UIApplication sendEvent:] + 38
18 UIKit 0x34f38c4c _UIApplicationHandleEvent + 5084
19 GraphicsServices 0x35873e70 PurpleEventCallback + 660
20 GraphicsServices 0x35873efa PurpleEventSignalCallback + 10
21 CoreFoundation 0x36126a72 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 6
22 CoreFoundation 0x36128758 __CFRunLoopDoSources0 + 376
23 CoreFoundation 0x361294e4 __CFRunLoopRun + 224
24 CoreFoundation 0x360b9ebc CFRunLoopRunSpecific + 224
25 CoreFoundation 0x360b9dc4 CFRunLoopRunInMode + 52
26 GraphicsServices 0x35873418 GSEventRunModal + 108
27 GraphicsServices 0x358734c4 GSEventRun + 56
28 UIKit 0x34f63d62 -[UIApplication _run] + 398
29 UIKit 0x34f61800 UIApplicationMain + 664
30 MyApp 0x000026c4 main (main.m:14)
31 MyApp 0x0000266c start + 32
EXC_BAD_ACCESS generally means you released an object then called that object later. Try enabling NSZombies to find it. There are numerous tutorials on how to do it. Here's one to start http://www.touch-code-magazine.com/how-to-debug-exc_bad_access/

Resources