.NET 7 iOS应用启动崩溃:执行原生代码时触发SIGABRT
问题描述
将Xamarin.iOS应用迁移至.NET 7后,应用启动时直接崩溃,且新建空白iOS项目也出现相同问题。
崩溃日志
================================================================= Native Crash Reporting ================================================================= Got a SIGABRT while executing native code. This usually indicates a fatal error in the mono runtime or one of the native libraries used by your application. ================================================================= ================================================================= Basic Fault Address Reporting ================================================================= Memory around native instruction pointer (0x1d8e3101c):0x1d8e3100c ff 0f 5f d6 c0 03 5f d6 10 29 80 d2 01 10 00 d4 .._..._..)...... 0x1d8e3101c 03 01 00 54 7f 23 03 d5 fd 7b bf a9 fd 03 00 91 ...T.#...{...... 0x1d8e3102c e8 dd ff 97 bf 03 00 91 fd 7b c1 a8 ff 0f 5f d6 .........{...._. 0x1d8e3103c c0 03 5f d6 7f 23 03 d5 ff c3 00 d1 f4 4f 01 a9 .._..#.......O.. ================================================================= Managed Stacktrace: ================================================================= at <unknown> <0xffffffff> at System.Environment:FailFast <0x00028> at System.Environment:FailFast <0x00018> at System.SR:InternalGetResourceString <0x001ba> at System.SR:GetResourceString <0x00044> at System.SR:get_Arg_DllNotFoundException <0x00010> at System.DllNotFoundException:.ctor <0x00010> at System.Object:runtime_invoke_direct_void__this__ <0x00080> at <unknown> <0x00000> at <unknown> <0xffffffff> at Sys:<GetEnv>g____PInvoke|0_0 <0x0004a> at Sys:GetEnv <0x0009c> at System.Environment:GetEnvironmentVariableCore <0x00028> at System.Environment:GetEnvironmentVariable <0x00026> at System.AppContextConfigHelper:GetBooleanConfig <0x00032> at Settings:.cctor <0x00046> at System.Object:runtime_invoke_direct_void <0x0007a> at <unknown> <0x00000> at <unknown> <0xffffffff> at System.Globalization.GlobalizationMode:get_Invariant <0x0000a> at System.Resources.ManifestBasedResourceGroveler:GetNeutralResourcesLanguage <0x00036> at System.Resources.ResourceManager:CommonAssemblyInit <0x000b2> at System.Resources.ResourceManager:.ctor <0x000ce> at System.SR:get_ResourceManager <0x00028> at System.SR:InternalGetResourceString <0x002ae> at System.SR:GetResourceString <0x00044> at System.SR:get_Arg_DllNotFoundException <0x00010> at System.DllNotFoundException:.ctor <0x00010> at System.Object:runtime_invoke_direct_void__this__ <0x00080> at <unknown> <0x00000> at <unknown> <0xffffffff> at Sys:Malloc <0x0004a> at System.Runtime.InteropServices.NativeMemory:Alloc <0x00040> at System.Runtime.InteropServices.Marshal:AllocHGlobal <0x00010> at System.Runtime.InteropServices.Marshal:AllocHGlobal <0x00016> at System.Runtime.InteropServices.Marshal:StringToHGlobalUTF8 <0x00074> at System.Runtime.InteropServices.Marshal:StringToHGlobalAuto <0x00010> at ObjCRuntime.TransientString:.ctor <0x0003c> at ObjCRuntime.Class:objc_getClass <0x00020> at ObjCRuntime.Class:GetHandle <0x00010> at Foundation.NSObject:.cctor <0x00030> at System.Object:runtime_invoke_direct_void <0x0007a> at <unknown> <0x00000> at <unknown> <0xffffffff> at ObjCRuntime.Runtime:Initialize <0x0016e> at <Module>:runtime_invoke_direct_void_Runtime/InitializationOptions* <0x00086> at <unknown> <0x00000> =================================================================
项目.csproj配置
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net7.0-ios</TargetFramework> <OutputType>Exe</OutputType> <Nullable>disable</Nullable> <ImplicitUsings>false</ImplicitUsings> <SupportedOSPlatformVersion>11.0</SupportedOSPlatformVersion> <UseInterpreter>true</UseInterpreter> <RuntimeIdentifiers>ios-arm64</RuntimeIdentifiers> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <CreatePackage>false</CreatePackage> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <CreatePackage>false</CreatePackage> </PropertyGroup> </Project>
环境信息
- Visual Studio for Mac 17.6.5(最新版本)
- Xcode 15,已尝试降级至14.3,问题依旧
排查与修复步骤
从堆栈日志看,崩溃根源是DllNotFoundException触发的Environment.FailFast,属于运行时初始化阶段的原生依赖加载异常,可按以下步骤解决:
移除
UseInterpreter配置
在项目PropertyGroup中删除<UseInterpreter>true</UseInterpreter>,解释器模式在部分环境下会导致原生依赖加载异常,改用默认的JIT/AOT编译模式。更新.NET 7 iOS工作负载
打开终端执行命令更新iOS工作负载,确保版本与VS Mac兼容:dotnet workload update ios清理项目与IDE缓存
- 删除项目目录下的
bin、obj文件夹 - 打开Visual Studio for Mac,依次点击
Visual Studio菜单 ->Preferences->Locations,点击Clear按钮清理IDE缓存 - 重启IDE后重新构建项目
- 删除项目目录下的
验证.NET SDK版本
执行dotnet --version查看当前.NET 7版本,若不是最新补丁版本,执行dotnet update更新SDK至最新。检查系统环境变量
堆栈显示调用System.Environment.GetEnvironmentVariable时出错,排查系统中是否存在损坏的.NET、Xcode相关环境变量,必要时重置环境变量。
内容的提问来源于stack exchange,提问作者Matan
相关产品推荐
相关产品推荐

