Crash in ResolvedStyledText.draw

Hi!

We're trying to investigate the following crash, but have a little bit hard time of understanding what the reason for it might be. I assume this might be us trying to reduce the state too often?

Thread 0 Crashed:
0   libswiftCore.dylib            	0x00000001884fec20 _swift_release_dealloc + 48 (:-1)
1   libswiftCore.dylib            	0x00000001884fd938 bool swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1>>::doDecrementSlow<(swift::PerformDeinit)1>(swift::RefCountBitsT<(swift::RefCountInlinedness)1>, unsigned int) + 240 (:-1)
2   SwiftUICore                   	0x000000018ed7f940 specialized _ArrayBuffer._consumeAndCreateNew(bufferIsUnique:minimumCapacity:growForAppend:) + 244 (:-1)
3   SwiftUICore                   	0x000000018ed7ecdc specialized Array._createNewBuffer(bufferIsUnique:minimumCapacity:growForAppend:) + 16 (:-1)
4   SwiftUICore                   	0x000000018ed7ecdc specialized Array._reserveCapacityAssumingUniqueBuffer(oldCount:) + 24 (:-1)
5   SwiftUICore                   	0x000000018ed7ecdc specialized Array.append(_:) + 56 (:-1)
6   SwiftUICore                   	0x000000018ed7ecdc NSAttributedString.MetricsCache.metrics(requestedSize:layoutMargins:wantsNumberOfLineFragments:context:) + 2996 (:-1)
7   SwiftUICore                   	0x000000018eda7afc ResolvedStyledText.StringDrawing.draw(in:with:applyingMarginOffsets:containsResolvable:context:renderer:) + 208 (:-1)
8   SwiftUICore                   	0x000000018ee5fe90 ResolvedStyledText.draw(in:with:applyingMarginOffsets:context:renderer:) + 100 (:-1)
9   SwiftUICore                   	0x000000018ee5fe90 closure #1 in DisplayList.ViewUpdater.Platform.updateItemView(_:index:item:state:) + 176 (:-1)
10  SwiftUICore                   	0x000000018ee5f7a0 closure #1 in PlatformDrawableContent.draw(in:size:contentsScale:state:) + 20 (:-1)
11  SwiftUICore                   	0x000000018ee5f7a0 CGContextRef.withPlatformContext(do:) + 56 (:-1)
12  SwiftUICore                   	0x000000018ee5f7a0 PlatformDrawableContent.draw(in:size:contentsScale:state:) + 136 (:-1)
13  SwiftUICore                   	0x000000018ee5fd94 CGDrawingLayer.draw(in:) + 88 (:-1)
14  SwiftUICore                   	0x000000018ee5fd94 @objc CGDrawingLayer.draw(in:) + 224 (:-1)
15  QuartzCore                    	0x000000018ad2304c CABackingStoreUpdate_ + 344 (:-1)
16  QuartzCore                    	0x000000018ad22df0 invocation function for block in CA::Layer::display_() + 72 (:-1)
17  QuartzCore                    	0x000000018ad257c4 CA::Layer::display_() + 2180 (:-1)
18  QuartzCore                    	0x000000018ad257c4 -[CALayer _display] + 2312 (:-1)
19  QuartzCore                    	0x000000018ac5d0f4 CA::Layer::display_if_needed(CA::Transaction*) + 256 (:-1)
20  QuartzCore                    	0x000000018ac5d0f4 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 732 (:-1)
21  QuartzCore                    	0x000000018ac593c8 CA::Context::commit_transaction(CA::Transaction*, double, double*) + 568 (:-1)
22  QuartzCore                    	0x000000018ac587c0 CA::Transaction::commit() + 428 (:-1)
23  QuartzCore                    	0x000000018ac59018 CA::Transaction::flush() + 84 (:-1)
24  QuartzCore                    	0x000000018ac585c0 CA::Transaction::flush_as_runloop_observer(bool) + 224 (:-1)
25  UIKitCore                     	0x000000018b111800 _UIApplicationFlushCATransaction + 52 (:-1)
26  UIKitCore                     	0x000000018b110e74 performCATransactionCommitAction + 308 (:-1)
27  UIKitCore                     	0x000000018b110e74 __setupUpdateSequence_block_invoke_2 + 336 (:-1)
28  UIKitCore                     	0x000000018b117b88 _UIUpdateSequenceRunNext + 128 (:-1)
29  UIKitCore                     	0x000000018b117928 schedulerStepScheduledMainSectionContinue + 48 (:-1)
30  UpdateCycle                   	0x00000002b9c47bc0 UC::DriverCore::continueProcessing() + 84 (:-1)
31  CoreFoundation                	0x0000000186ea8c20 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28 (:-1)
32  CoreFoundation                	0x0000000186ea8b94 __CFRunLoopDoSource0 + 192 (:-1)
33  CoreFoundation                	0x0000000186e8a268 __CFRunLoopDoSources0 + 316 (:-1)
34  CoreFoundation                	0x0000000186e89384 __CFRunLoopRun + 416 (:-1)
35  CoreFoundation                	0x0000000186e8a7f4 _CFRunLoopRunSpecificWithOptions + 344 (:-1)
36  GraphicsServices              	0x000000023154cf24 GSEventRunModal + 120 (:-1)
37  UIKitCore                     	0x000000018b1681cc UIApplicationMain + 336 (:-1)
38  UIKitCore                     	0x000000018b403644 UIApplicationMain(_:_:_:_:) + 104 (:-1)
39  MyApp                       	0x00000001021d9c08 main + 116 (/<compiler-generated>:10)
40  MyApp                       	0x00000001021d9c08 0x1021d4000 + 23560 (AppDelegate.swift:0)
41  dyld                          	0x0000000186cdb5b8 start + 6616 (:-1)

Attaching one of the crashes, but I do have an access to xcrashpoint file as well. Crash:

It seems to happen on the following version of iOS:

  • 18.7.8
  • 26.5
  • 26.6.2
  • 26.7.1
  • 27.0
  • 27.0.1

Would appreciate any tips!

Hello Scana,

This crash appears to be occurring when CATransaction calls flush() during the Core Animation draw cycle on the main thread. While SwiftUI is text rendering, reference count corruption (_swift_release_dealloc) happens in the array manipulations of a cache lookup. The crash happens during SwiftUI's internal text rendering and layout caching in a draw pass. I'm guessing there's some sort of threading issue where a background thread is modifying the mutable state of a text view.

Your best bet is to enable Thread Sanitizer if you can replicate this crash on device, to get a more precise root cause. You should also check to see if there's any background thread mutations of any SwiftUI text views, and make sure your view models use @MainActor isolation.

Hoping this helps,

Richard Yeh  Developer Technical Support

Crash in ResolvedStyledText.draw
 
 
Q