-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Unexpected behavior with nested [InlineArray] #97603
Comments
Tagging subscribers to this area: @dotnet/area-system-buffers Issue DetailsDescriptionI'm seeing random data when nesting a ref struct with an [InlineArray] in another ref struct. Reproduction StepsGiven the following type definitions:
I get the following behavior: FixedBuffer16<Point> buffer1 = new();
buffer1[0] = new(1, 2);
// value is (1, 2)
var value = buffer1[0];
FixedPointBuffer buffer2 = new();
buffer2[0] = new(3, 4);
// value is random
value = buffer2[0]; Why would the first case work and the second not? Am I missing some detail on how things work with refs? Is this a bug? Expected behaviorWould expect that this would just work. Actual behaviorGarbage from the nesting type. Regression?No response Known WorkaroundsNo response Configuration.NET 8 Other informationNo response
|
To reliably do something like this you probably need to make the constructors static methods instead and have them take explicit Edit: @reflectronic points out that |
The problem here is much more obvious if you attempt to do this: static FixedBuffer16<T> CreateBuffer<T>() => new(); You are creating a struct that contains a reference to its own original construction location, in FixedPointBuffer (depending on optimization level) that would be its constructor's stackframe, leading you to reference a location on the stack that is no longer valid, and can therefore be overwritten at any time. |
By design according to the linked issues. |
Description
I'm seeing random data when nesting a ref struct with an [InlineArray] in another ref struct.
Reproduction Steps
Given the following type definitions:
I get the following behavior:
Why would the first case work and the second not? Am I missing some detail on how things work with refs? Is this a bug?
Expected behavior
Would expect that this would just work.
Actual behavior
Garbage from the nesting type.
Regression?
No response
Known Workarounds
No response
Configuration
.NET 8
Other information
No response
The text was updated successfully, but these errors were encountered: