Swift崩溃日志指向CustomClass.swift不存在的25行,排查真实崩溃根因
崩溃问题提问
各位StackOverflow技术排查社区的同仁:
数月来我始终在排查一类疑难崩溃问题,这类崩溃的日志指向的代码行,在对应的源文件中根本不存在。
在下方提供的崩溃日志中,CustomClass.swift文件确实真实存在,但崩溃标注的第25行在该文件中并不存在。想请教造成这类崩溃的真实原因是什么?
补充背景
- 我们在后台运行计算强度较高的任务
- 该任务由C++编写的自定义框架(即日志中的
OUR_CUSTOM_FRAMEWORK)实现 - 运行时会分配约700MB的内存
崩溃日志
Exception Type: EXC_CRASH (SIGABRT) Exception Codes: 0x0000000000000000, 0x0000000000000000 Exception Note: EXC_CORPSE_NOTIFY Triggered by Thread: 0 Thread 0 name: Thread 0 Crashed: 0 libsystem_kernel.dylib 0x00000001934c6df0 __pthread_kill + 8 1 libsystem_pthread.dylib 0x00000001933e6930 pthread_kill + 228 (pthread.c:1458) 2 libsystem_c.dylib 0x0000000193374ba4 abort + 104 (abort.c:110) 3 libsystem_malloc.dylib 0x00000001933d7fdc malloc_vreport + 564 (malloc_printf.c:183) 4 libsystem_malloc.dylib 0x00000001933d81a4 malloc_report + 64 (malloc_printf.c:192) 5 libsystem_malloc.dylib 0x00000001933cbd1c free + 436 (malloc.c:1733) 6 [OUR_CUSTOM_FRAMEWORK] 0x000000010324ccf8 0x1031a4000 + 691448 7 libsystem_c.dylib 0x0000000193355164 __cxa_finalize_ranges + 416 (atexit.c:284) 8 libsystem_c.dylib 0x00000001933554a0 exit + 28 (exit.c:81) 9 UIKitCore 0x000000019782eb88 -[UIApplication _terminateWithStatus:] + 508 (UIApplication.m:6735) 10 UIKitCore 0x0000000196f97718 -[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:] + 128 (_UISceneLifecycleMultiplexer.m:765) 11 UIKitCore 0x0000000196f9737c -[_UISceneLifecycleMultiplexer forceExitWithTransitionContext:scene:] + 220 (_UISceneLifecycleMultiplexer.m:418) 12 UIKitCore 0x0000000197824ac4 -[UIApplication workspaceShouldExit:withTransitionContext:] + 216 (UIApplication.m:3725) 13 FrontBoardServices 0x000000019893bcf8 -[FBSUIApplicationWorkspaceShim workspaceShouldExit:withTransitionContext:] + 88 (FBSUIApplicationWorkspace.m:144) 14 FrontBoardServices 0x0000000198968d68 __83-[FBSWorkspaceScenesClient willTerminateWithTransitionContext:withAcknowledgement:]_block_invoke_2 + 80 (FBSWorkspaceScenesClient.m:281) 15 FrontBoardServices 0x000000019894debc -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 240 (FBSWorkspace.m:357) 16 FrontBoardServices 0x0000000198968cf4 __83-[FBSWorkspaceScenesClient willTerminateWithTransitionContext:withAcknowledgement:]_block_invoke + 140 (FBSWorkspaceScenesClient.m:278) 17 libdispatch.dylib 0x000000019338033c _dispatch_client_callout + 20 (object.m:495) 18 libdispatch.dylib 0x00000001933830d4 _dispatch_block_invoke_direct + 264 (queue.c:466) 19 FrontBoardServices 0x000000019898f6c4 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 48 (FBSSerialQueue.m:173) 20 FrontBoardServices 0x000000019898f370 -[FBSSerialQueue _queue_performNextIfPossible] + 432 (FBSSerialQueue.m:216) 21 FrontBoardServices 0x000000019898f8dc -[FBSSerialQueue _performNextFromRunLoopSource] + 32 (FBSSerialQueue.m:247) 22 CoreFoundation 0x000000019365baf4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28 (CFRunLoop.c:1922) 23 CoreFoundation 0x000000019365ba48 __CFRUNLOOPDoSource0 + 84 (CFRunLoop.c:1956) 24 CoreFoundation 0x000000019365b198 __CFRUNLOOPDoSources0 + 196 (CFRunLoop.c:1992) 25 CoreFoundation 0x0000000193655f38 __CFRunLoopRun + 796 (CFRunLoop.c:2882) 26 CoreFoundation 0x00000001936558f4 CFRunLoopRunSpecific + 480 (CFRunLoop.c:3192) 27 GraphicsServices 0x000000019da6c604 GSEventRunModal + 164 (GSEvent.c:2246) 28 UIKitCore 0x0000000197829358 UIApplicationMain + 1944 (UIApplication.m:4823) 29 [APPNAME] 0x0000000100d45128 main + 68 (CustomClass.swift:25) 30 libdyld.dylib 0x00000001934d12dc start + 4
问题排查答案
核心判定
- 这是典型的 堆内存损坏(Heap Corruption) 延迟触发崩溃,日志标注的
CustomClass.swift:25完全不是真实崩溃位置,和Swift侧代码无关。 - 行号无法对应你的源文件的原因:
- 崩溃实际发生在进程退出阶段的C++全局对象析构流程,符号化工具回溯栈时,直接把最外层Swift包装入口函数的行号当成了崩溃点
- 也有可能你用来符号化的dSYM文件和本地当前的
CustomClass.swift版本不匹配,行号映射错位
崩溃栈关键链路解析
从触发崩溃的栈帧从上到下拆解:
0~2:标准的abort终止进程调用链3~5:malloc库的主动校验逻辑——执行free()时发现堆元数据被篡改,主动触发abort6:你的OUR_CUSTOM_FRAMEWORK调用free的具体位置7~8:__cxa_finalize_ranges→exit,明确说明这个free调用属于进程退出时的全局C++对象析构流程,不是App正常运行时的业务逻辑9往下:UIKit收到系统退出指令,走完正常的App退出流程,最终栈回溯到Swift编译生成的main入口函数,因此被标注了CustomClass.swift:25这个错位行号
结合你的业务背景的根因匹配
你提到C++框架在后台跑高负载任务,单次分配约700MB内存,完全匹配这个崩溃场景:
- C++代码存在堆越界写、use-after-free、重复释放这类问题,悄悄破坏了malloc维护的堆块元数据
- 这类损坏在App正常运行时不会立刻被检测,直到进程收到退出指令,开始执行全局对象析构,调用到C++框架中某个对象的析构函数、执行
free时,malloc才发现异常,终止进程 - 因为崩溃发生在退出阶段,符号化时的栈回溯基准是main入口,才会出现和源文件对不上的错误行号
排查建议
- 直接用
Address Sanitizer(ASan)跑C++框架的高负载任务,ASan会在堆越界写的第一现场就捕获错误,不用等退出阶段才触发abort - 重点检查C++框架中700MB内存的分配、访问、释放逻辑,尤其是全局静态对象的析构流程
- 不用排查Swift侧
CustomClass.swift的代码,这个行号是完全错位的结果
内容的提问来源于stack exchange,提问作者da1
相关产品推荐
相关产品推荐

