Skip to content

Proposal: use span and ref-based interop instead of GetPointer #3976

Open

Description

@sasvdw

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], or ref/in of a value) to the generic ref/ref readonly overloads that Silk.NET generates for native calls. These overloads pin the reference with fixed for the duration of the call.
  • Use fixed only where a pointer has to stay valid for longer than one call, and keep the pointer inside the fixed block.
  • Helpers that fill a buffer write into a Span<T> and return a length or offset, not a pointer into it.

Proposed changes

  1. Mark both GetPointer overloads [Obsolete], with a message that points to the idiom above. Remove them in a later major version. They are public in Stride.Core, so removing them now would break user code.
  2. Change the remaining call sites:
    • PipelineState.Direct3D11.EncodeSemanticName(string, ref Span<byte>) returns a pointer into its buffer. Its only caller passes a stackalloc buffer, 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 its stackalloc buffer to get the addresses.
    • StringMarshal.GetString(this ReadOnlySpan<byte>), GetString(this ReadOnlySpan<ushort>) and GetString(this ReadOnlySpan<char>) use GetPointer() only to check for null; 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 return null is a behaviour change and a separate decision.
  3. Review other conversions with the same shape, for example StringMarshal.GetUtf8Span(in byte source, int maxLength), which builds a span from Unsafe.AsPointer when the length is unknown. Its byte* 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 public Stride.Core method?
  • Should StringMarshal.GetString return null for an empty span, or only for a span with no reference (default)?

Activity

  1. added
    area-Core
    enhancement
    on Nov 15, 2026
  2. xen2 commented on Nov 16, 2026

    @xen2
    Member

    Thanks for the write-up, the analysis of #3571 (the D3D11 UpdateSubResource stale-memory bug) matches what we see.

    On the first open question: I'd skip the [Obsolete] phase and remove both GetPointer overloads in 4.5, in the same PR that changes the call sites.

    • They only shipped in 4.4.x (added in 4.4.0-beta1).
    • I found no use outside the engine. GitHub code search only finds Stride's own sources and two repos that copy them. There is nothing in the samples or templates either.
    • A code upgrader can't do this one safely.
    • They're dangerous and likely to enable same kind of execution exception in user code (if ever used)

    On the second question: I'd keep the current behaviour, and not only for this change. null for a span with no reference (default) and "" for an empty span mirrors the pointer overloads and GetUtf8Span, so null and "" both survive a round trip through native code, where a null char* and an empty string mean different things. Returning null for an empty span would turn real empty names (for example in the D3D shader reflection) into null. Unsafe.IsNullRef(ref MemoryMarshal.GetReference(span)) gives exactly the current result. The <returns> docs of the three span overloads should say when they return null.

    The EncodeSemanticName signature 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.

  3. sasvdw commented on Nov 16, 2026

    @sasvdw
    ContributorAuthor

    Thanks. I changed #3977 to match:

    • Both GetPointer overloads are removed, not marked obsolete.
    • GetString keeps its result for default and empty spans, and the <returns> docs of the three span overloads say when they return null.
    • EncodeSemanticName has the signature above.

    GetUtf8Span(in byte, int) is not in it. #3977 contains #3573's commits until that merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Corearea-Graphicsenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions