You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm currently building a managed wrapper for a native library. A parameter in one of the API functions includes an embedded structure with callback information. I modeled the corresponding C# type to mirror its native definition using delegates. However, I observe that while the P/Invoke call using this parameter succeeds and that the delegate is marshaled correctly, it crashes upon return.
Reproduction Steps
The issue can be reproduced with the code included below that manually marshals and then destroys the structs.
Struct Foo2 is a simplified definition of the API parameter. It contains a nested CallbackData member that contains the delegate. For comparison, struct Foo1 flattens the same members to remove the embedded structure but that, presumably, has an identical memory layout.
Struct Foo1 can be marshaled successfully while Foo2 fails to be destroyed and results in a NullReferenceException in Marshal.DestroyStructure.
Object reference not set to an instance of an object.
at System.Runtime.InteropServices.Marshal.DestroyStructure(IntPtr ptr, Type structuretype)
at System.Runtime.InteropServices.Marshal.DestroyStructure[T](IntPtr ptr)
at Benchmark.DestroyStructureBug.TestMarshaling[T](T value) in ...
Regression?
No response
Known Workarounds
Flatten the embedded struct members or use a function pointer instead of the delegate.
Configuration
.NET SDK (reflecting any global.json):
Version: 6.0.100
Commit: 9e8b04bbff
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19043
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.100\
Host (useful for support):
Version: 6.0.0
Commit: 4822e3c
Other information
No response
The text was updated successfully, but these errors were encountered:
Description
I'm currently building a managed wrapper for a native library. A parameter in one of the API functions includes an embedded structure with callback information. I modeled the corresponding C# type to mirror its native definition using delegates. However, I observe that while the P/Invoke call using this parameter succeeds and that the delegate is marshaled correctly, it crashes upon return.
Reproduction Steps
The issue can be reproduced with the code included below that manually marshals and then destroys the structs.
Struct
Foo2
is a simplified definition of the API parameter. It contains a nestedCallbackData
member that contains the delegate. For comparison, structFoo1
flattens the same members to remove the embedded structure but that, presumably, has an identical memory layout.Expected behavior
The
Foo2
type should be destroyed correctly.Actual behavior
Struct
Foo1
can be marshaled successfully whileFoo2
fails to be destroyed and results in a NullReferenceException inMarshal.DestroyStructure
.Regression?
No response
Known Workarounds
Flatten the embedded struct members or use a function pointer instead of the delegate.
Configuration
.NET SDK (reflecting any global.json):
Version: 6.0.100
Commit: 9e8b04bbff
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19043
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.100\
Host (useful for support):
Version: 6.0.0
Commit: 4822e3c
Other information
No response
The text was updated successfully, but these errors were encountered: