如何修复QuartzCore中CA::Layer::sublayers()崩溃问题?
iOS应用崩溃排查方向
部分用户反馈应用出现崩溃,但本地无法复现该故障。以下是从Crashlytics获取的堆栈跟踪信息,据此整理出具体排查方向:
Crashed: com.apple.main-thread 0 QuartzCore 0x5d340 CA::Layer::sublayers() + 4 1 UIKitCore 0x40d84 __88-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:]_block_invoke + 116 2 UIKitCore 0x40de8 __88-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:]_block_invoke + 216 3 UIKitCore 0x40de8 __88-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:]_block_invoke + 216 4 UIKitCore 0x40de8 __88-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:]_block_invoke + 216 5 UIKitCore 0x40de8 __88-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:]_block_invoke + 216 6 UIKitCore 0x40de8 __88-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:]_block_invoke + 216 7 UIKitCore 0x40de8 __88-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:]_block_invoke + 216 8 UIKitCore 0x40de8 __88-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:]_block_invoke + 216 9 UIKitCore 0x40de8 __88-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:]_block_invoke + 216 10 UIKitCore 0x40de8 __88-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:]_block_invoke + 216 11 UIKitCore 0x40de8 __88-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:]_block_invoke + 216 12 UIKitCore 0x55104 -[UIView _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:] + 216 13 UIKitCore 0x49120 -[UIView _didChangeFromIdiomOnScreen:traverseHierarchy:] + 112 14 UIKitCore 0x420578 -[UIWindow _didMoveFromScreen:toScreen:] + 684 15 UIKitCore 0x2a2c18 -[UIWindow _didMoveFromScene:toScene:] + 296 16 UIKitCore 0x2a29e0 -[UIWindowScene _attachWindow:] + 140 17 UIKitCore 0x2a4b34 -[_UIScreenBasedWindowScene _attachWindow:] + 52 18 UIKitCore 0x343790 -[UIWindow _setWindowHostingScene:] + 196 19 UIKitCore 0x34368c -[UIWindow setWindowScene:] + 212 20 UIKitCore 0x69e714 _UIApplicationSceneDisconnectionHandler_block_invoke_3 + 688 21 UIKitCore 0x4a5c60 -[UIScene _invalidate] + 804 22 UIKitCore 0x508da0 -[UIWindowScene _invalidate] + 148 23 UIKitCore 0x47235c -[UIApplication workspace:willDestroyScene:withTransitionContext:completion:] + 180 24 UIKitCore 0x46053c -[UIApplicationSceneClientAgent scene:willInvalidateWithEvent:completion:] + 288 25 FrontBoardServices 0x1c950 -[FBSScene _callOutQueue_agent_willDestroyWithTransitionContext:completion:] + 236 26 FrontBoardServices 0x20f4c __84-[FBSWorkspaceScenesClient _queue_invalidateScene:withTransitionContext:completion:]_block_invoke_2 + 120 27 FrontBoardServices 0x7120 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 168 28 FrontBoardServices 0x1b5cc __84-[FBSWorkspaceScenesClient _queue_invalidateScene:withTransitionContext:completion:]_block_invoke + 292 29 libdispatch.dylib 0x3eac _dispatch_client_callout + 20 30 libdispatch.dylib 0x791c _dispatch_block_invoke_direct + 264 31 FrontBoardServices 0x1124c __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 52 32 FrontBoardServices 0x10de8 -[FBSSerialQueue _targetQueue_performNextIfPossible] + 220 33 FrontBoardServices 0x13694 -[FBSSerialQueue _performNextFromRunLoopSource] + 28 34 CoreFoundation 0xd3128 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28 35 CoreFoundation 0xdf7b4 __CFRunLoopDoSource0 + 176 36 CoreFoundation 0x645e8 __CFRunLoopDoSources0 + 244 37 CoreFoundation 0x7a0d4 __CFRunLoopRun + 828 38 CoreFoundation 0x7f3ec CFRunLoopRunSpecific + 612 39 GraphicsServices 0x135c GSEventRunModal + 164 40 UIKitCore 0x39cf58 -[UIApplication _run] + 888 41 UIKitCore 0x39cbbc UIApplicationMain + 340 42 myApp-ios 0xae7c main + 24 (AppDelegate.swift:24) 43 ??? 0x1dd91cdec (Missing)
具体排查方向
- 检查视图层级中的野指针/已释放对象:崩溃发生在访问CALayer的sublayers时,大概率是某个UIView或CALayer已被释放但仍存在于视图层级中。重点关注场景切换(多窗口、分屏、App前后台切换)时,是否有异步释放UI组件但未从层级中移除的操作。
- 排查自定义视图/图层的内存管理:如果使用了自定义UIView或CALayer,检查dealloc方法是否正确移除子视图/图层,是否存在对象释放后仍被强引用持有的情况,避免内存混乱。
- 模拟场景切换/窗口迁移场景:尝试触发iPad分屏切换、多窗口管理、App后台强制终止重启、横竖屏切换等操作,复现崩溃场景。
- 确保UI操作线程安全:所有UI相关操作必须在主线程执行,排查是否存在后台线程修改视图层级(添加/移除子视图)的情况,避免主线程遍历层级时出错。
- 分析Crashlytics附加信息:查看崩溃设备的iOS版本、机型是否集中在特定范围,以及崩溃前的用户操作路径,缩小问题范围。
- 启用僵尸对象检测:在Xcode中开启
Zombie Objects(路径:Edit Scheme -> Diagnostics -> Enable Zombie Objects),复现时若访问已释放对象会直接报错,便于定位问题。 - 检查第三方UI库的视图操作:如果使用了第三方UI库,确认其在场景切换时是否有自动修改视图层级的逻辑,排查可能存在的内存管理问题。
内容的提问来源于stack exchange,提问作者Kakan_Aman
相关产品推荐
相关产品推荐

