-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[DllImportGenerator] Initialize by-value out array to default #61431
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to mentioned the discovered behavior around implicit out with the built-in in the compat doc?
@@ -71,6 +71,8 @@ Only single-dimensional arrays are supported for source generated marshalling. | |||
|
|||
In the source-generated marshalling, arrays will be allocated on the stack (instead of through [`AllocCoTaskMem`](https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.marshal.alloccotaskmem)) if they are passed by value or by read-only reference if they contain at most 256 bytes of data. The built-in system does not support this optimization for arrays. | |||
|
|||
In the built-in system, marshalling a `char` array by value with `CharSet.Unicode` would default to also marshalling data out. In the source-generated marshalling, the `char` array must be marked with the `[Out]` attribute for data to be marshalled out by value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is due to the fact that a char array with CharSet.Unicode used to be pinnacle as char is blittable with CharSet.Unicode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I figured that was it once I saw the behaviour, but it was not something I realized immediately 😕
When marshalling an array out by value, we were leaving the native storage uninitialized. This clears it out, so that we don't end up with whatever garbage was there in the array (matches built-in system behaviour).
This is the reason behind the failures in #61389.
@AaronRobinsonMSFT @jkoritzinsky