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
When creating an inline array via a constructor that assigns the elements of the array, accessing said inline array via a spread element throws a System.AccessViolationException.
Note that the AccessViolationException occurs only when using the spread element on the inline array which has at least one element assigned (this[i] = ...) in the constructor. This does not occur if the array elements are unassigned, nor when performing an implicit/explicit cast from the inline array to ReadOnlySpan<T>/Span<T>.
Expected behavior
The spread element should not throw an exception in this use-case.
Actual behavior
An exception of System.AccessViolationException is thrown when the inline array is accessed via the spread element (in a collection expression). Other uses of the inline array do not throw this exception.
Regression?
Unknown.
Known Workarounds
The inline array can be implicitly/explicitly cast to a span first, and then used by the spread element normally.
That is, rather than constructing a new span from the inline array, an unsafe cast is performed, incorrectly mapping the inline array onto the memory layout of a Span<T>:
Ah! Thanks for identifying the existing issue. It's somewhat of a difficult subject to search for, but hopefully the referenced StackOverflow question and workaround is useful to anyone else coming across this.
Description
When creating an inline array via a constructor that assigns the elements of the array, accessing said inline array via a spread element throws a
System.AccessViolationException
.Reproduction Steps
For an inline array defined as:
Then accessing the inline array such as:
Note that the
AccessViolationException
occurs only when using the spread element on the inline array which has at least one element assigned (this[i] = ...
) in the constructor. This does not occur if the array elements are unassigned, nor when performing an implicit/explicit cast from the inline array toReadOnlySpan<T>
/Span<T>
.Expected behavior
The spread element should not throw an exception in this use-case.
Actual behavior
An exception of
System.AccessViolationException
is thrown when the inline array is accessed via the spread element (in a collection expression). Other uses of the inline array do not throw this exception.Regression?
Unknown.
Known Workarounds
The inline array can be implicitly/explicitly cast to a span first, and then used by the spread element normally.
targetSpan
andtargetSpan2
do not throw an exception.Configuration
.NET 8
Visual Studio Community 2022 (64-bit) Version 17.8.1
Windows 11
VS version and OS do not seem to affect this issue.
Other information
Via StackOverflow, user Ivan Petrov reports that the spread element is compiled into the following:
That is, rather than constructing a new span from the inline array, an unsafe cast is performed, incorrectly mapping the inline array onto the memory layout of a
Span<T>
:Because the inline array does not match this layout in memory, the
AccessViolationException
is thrown.The text was updated successfully, but these errors were encountered: