Open ThadHouse opened 8 months ago
Tagging subscribers to this area: @dotnet/interop-contrib See info in area-owners.md if you want to be subscribed.
Author: | ThadHouse |
---|---|
Assignees: | - |
Labels: | `area-System.Runtime.InteropServices`, `untriaged` |
Milestone: | - |
Description
When using a stateful native marshaller, all created temporary variables are initialized to default(). However, when using stateless, all variables are left uninitialized. This can result in unexpected behavior changes by the caller, especially if they change from stateful to stateless.
Reproduction Steps
Compile the above code, and review the generated output. __status_native, which is passed as a pointer into the P/Invoke is initialized to default for the stateful generator, and not initialized at all for the stateless generator.
Expected behavior
I would expect these 2 to use the same type of initialization for the same variable generated between them.
Actual behavior
One is generated with initialization, one is not.
Regression?
No response
Known Workarounds
No response
Configuration
.NET 8
Other information
I noticed this because I had a scenario where I needed to ensure an
out
parameter was initialized to 0 because the native function required that, and was trying to find a method that did so. Switching to stateful did work, but adds a try catch and a ton of overhead that might not be worth it for my use case.