Repository navigation
Description
Problem
UnsafeUtilities.GetPointer(this Span<T>) and GetPointer(this ReadOnlySpan<T>) in Stride.Core return Unsafe.AsPointer(ref span.GetReference()). The span's reference is tracked by the GC: when a compacting GC moves an array, every span over it is updated. The pointer is not tracked, and nothing pins the memory. So the pointer is only valid while the memory can't move: stack memory, native memory, or an array that is already pinned.
A span doesn't say which kind of memory it covers, so a method that takes any span and returns a pointer can't know whether its result is safe. #3571 is the bug this causes: Direct3D 11 UpdateSubResource gave the driver a GetPointer address for a span over a managed array, and a GC during the native call made the driver read stale memory. #3573 fixes that call site.
Proposed idiom
Interop code passes managed memory to native code without converting it to a pointer by hand:
- Pass a reference to the first element (
in span[0], orref/inof a value) to the genericref/ref readonlyoverloads that Silk.NET generates for native calls. These overloads pin the reference withfixedfor the duration of the call. - Use
fixedonly where a pointer has to stay valid for longer than one call, and keep the pointer inside thefixedblock. - Helpers that fill a buffer write into a
Span<T>and return a length or offset, not a pointer into it.
Proposed changes
- Mark both
GetPointeroverloads[Obsolete], with a message that points to the idiom above. Remove them in a later major version. They are public inStride.Core, so removing them now would break user code. - Change the remaining call sites:
PipelineState.Direct3D11.EncodeSemanticName(string, ref Span<byte>)returns a pointer into its buffer. Its only caller passes astackallocbuffer, so it is safe today, but nothing stops a heap-backed span. Proposed signature:int EncodeSemanticName(string semanticName, Span<byte> semanticNameBytes), returning the number of bytes written; the caller pins or uses itsstackallocbuffer to get the addresses.StringMarshal.GetString(this ReadOnlySpan<byte>),GetString(this ReadOnlySpan<ushort>)andGetString(this ReadOnlySpan<char>)useGetPointer()only to check fornull; the pointer is never dereferenced, so there is no GC bug.Unsafe.IsNullRef(ref MemoryMarshal.GetReference(span))gives the same result without a pointer. Whether an empty span should also returnnullis a behaviour change and a separate decision.
- Review other conversions with the same shape, for example
StringMarshal.GetUtf8Span(in byte source, int maxLength), which builds a span fromUnsafe.AsPointerwhen the length is unknown. Itsbyte*overload forwards native memory to it, which can't move.
Open questions
- Is
[Obsolete]now and removal in the next major version the preferred path for a publicStride.Coremethod? - Should
StringMarshal.GetStringreturnnullfor an empty span, or only for a span with no reference (default)?
Activity
xen2 commentedon Nov 16, 2026
Thanks for the write-up, the analysis of #3571 (the D3D11
UpdateSubResourcestale-memory bug) matches what we see.On the first open question: I'd skip the
[Obsolete]phase and remove bothGetPointeroverloads in 4.5, in the same PR that changes the call sites.On the second question: I'd keep the current behaviour, and not only for this change.
nullfor a span with no reference (default) and""for an empty span mirrors the pointer overloads andGetUtf8Span, sonulland""both survive a round trip through native code, where a nullchar*and an empty string mean different things. Returningnullfor an empty span would turn real empty names (for example in the D3D shader reflection) intonull.Unsafe.IsNullRef(ref MemoryMarshal.GetReference(span))gives exactly the current result. The<returns>docs of the three span overloads should say when they returnnull.The
EncodeSemanticNamesignature you propose (int EncodeSemanticName(string, Span<byte>), returning the byte count) looks good to me.A PR with the removal and the call-site changes is welcome.
sasvdw commentedon Nov 16, 2026
Thanks. I changed #3977 to match:
GetPointeroverloads are removed, not marked obsolete.GetStringkeeps its result fordefaultand empty spans, and the<returns>docs of the three span overloads say when they returnnull.EncodeSemanticNamehas the signature above.GetUtf8Span(in byte, int)is not in it. #3977 contains #3573's commits until that merges.