-
-
Notifications
You must be signed in to change notification settings - Fork 226
Runtime crash when deserialising under NativeAOT #382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is a bug in native AOT, tracked at dotnet/runtime#113664. Once fixed, MemoryPack will run into the following exception:
That code is not trimming safe - native AOT optimizes away reflection metadata even if the method exists. I would suggest replacing the inexact1 interface method invocation with something trim and AOT safe, like: static bool TryInvokeRegisterFormatter<T>()
{
if (typeof(IMemoryPackFormatterRegister).IsAssignableFrom(typeof(T)))
{
// Will not trigger trimming or AOT warnings as of .NET 9 since the entire chain can be statically analyzed
// You will see an AOT warning if you're still on .NET 8, but that's purely a point in time issue
// More about intrinsically recognized AOT unsafe APIs: https://learn.microsoft.com/dotnet/core/deploying/native-aot/intrinsic-requiresdynamiccode-apis
typeof(MemoryPackFormatterProvider).GetMethod("InvokeRegisterFormatter").MakeGenericMethod(typeof(T)).Invoke(null, null);
}
}
static void InvokeRegisterFormatter<T>() where T : IMemoryPackFormatterRegister
{
T.RegisterFormatter();
} I would also recommend marking the project with Footnotes
|
@neuecc Would it be possible to get these fixes rolled into MemoryPack itself and perhaps have a warning about this in the readme? |
When attempting to deserialise a class containing a List of structs deriving from an interface (all tagged according to the MemoryPack documentation, i.e. with
[MemoryPackUnion(tag, type)]
), when compiling under NativeAOT, the following crash occurs:This is after ensuring both a Serialize and Deserialize method are available (per #189 , in this case just with a dirty workaround by making a useless call to Serialize).
The text was updated successfully, but these errors were encountered: