ArrayPool<T>.Shared Buffer Clearing and Cross-Instance Return
0 reputation · 09 Dec 2020, 01:28 UTC
The goal is to rent buffers from ArrayPool<T>.Shared and return them without retaining memory outside the pool. The API does not require that an array be returned to the same ArrayPool<T> instance that rented it, and it does not detect duplicate Return calls. The clearArray parameter defaults to false, so prior contents can remain visible to the next renter.
Behavior assumes .NET 6 or later; Shared pool limits are runtime-default and version-sensitive. It is unclear whether returning a buffer to a different pool instance than the one that produced it is supported, and whether clearArray: true should be mandatory for buffers that held sensitive data.
- Does returning a
T[]to a differentArrayPool<T>instance than the one that rented it violate the pool contract? - Should
clearArray: truebe required by default for pooled buffers containing sensitive data? - Are duplicate
Returncalls on the same array undefined behavior across .NET versions?