Is there a way to store managed pointers in a stackalloc buffer without incurring unexpected behavior from the GC? [duplicate]
This question already has answers here: Why stackalloc cannot be used with reference types? (4 answers) Closed 9 hours ago. This post was edited and submitted for review 14 mins ago. I’m working with C# in a game engine and frequently need to pass around subsets of collections of objects to different synchronous functions. I […]
Reinterpreting a stack allocated buffer to store managed pointers – will they interfere with GC?
I’m working with C# in a game engine and frequently need to pass around collections of objects to different functions. I’m running into cases where I only need to pass a subset of entries from some heap allocated collection, usually in the single to low double-digits. I am explicitly attempting to avoid allocating a new heap array for each call I make. I hoped it would be as simple as using stackalloc to make a small buffer wrapped by a Span and copy the references in, then pass that wherever it’s needed. This of course doesn’t work, as the the docs state that stackalloc can only be used with unmanaged types.