🔧 The IDisposable Struct That Never Got Disposed
A struct implementing IDisposable is often chosen specifically to avoid a heap allocation, and that works fine as long as every piece of code touching it knows its concrete type. The moment it’s stored in a variable typed as the interface, or passed into a method whose parameter type is IDisposable rather than the struct itself, the runtime boxes it: a brand-new heap object is created to hold a copy of the struct’s value. Disposing through that interface reference disposes the boxed copy, not the original struct sitting in whatever variable you actually care about, so a using block that looks perfectly correct can leave the real resource – a rented buffer, a pooled handle – never released at all.
🔎 The Problem
public struct PooledBuffer : IDisposable
{
private byte[] _buffer;
public PooledBuffer(int size) { _buffer = ArrayPool<byte>.Shared.Rent(size); }
public void Dispose()
{
if (_buffer != null) { ArrayPool<byte>.Shared.Return(_buffer); _buffer = null; }
}
}
void ReleaseResource(IDisposable resource)
{
// 'resource' is a BOXED COPY of whatever struct was passed in -
// disposing it releases that copy's buffer field, not necessarily
// the caller's original struct's buffer, once boxing has happened.
resource.Dispose();
}
var buffer = new PooledBuffer(1024);
ReleaseResource(buffer); // 'buffer' got boxed to satisfy IDisposable here
// The original local 'buffer' variable was never disposed by this call.
✅ Fix: Never Let the Struct Cross an Interface Boundary
- Never pass a disposable struct through a parameter or variable typed as the interface; keep the signature using the concrete struct type (or a generic method constrained to IDisposable), so the compiler calls Dispose directly on the struct value without ever boxing it.
- If a struct genuinely needs to flow through code that only knows about IDisposable, treat that as a sign it should probably be a class instead – the boxing here isn’t a minor performance detail, it’s a correctness bug, because the boxed copy and the original stop being the same storage the instant the box is created.
- Audit specifically for pooled-resource structs (ArrayPool-backed buffers and similar), since the entire point of making them structs was usually avoiding an allocation in the first place, and any boxing path quietly reintroduces that exact allocation on top of disposing the wrong copy.
⚠️ Why This Is Easy to Miss
- The boxing happens at a call site that looks completely ordinary – passing a struct into a method parameter – with no explicit cast, no visible new, nothing in the source that signals an allocation or a copy is being made at that line.
- using (var buffer = new PooledBuffer(…)) still compiles and still calls Dispose() on something, so the code gives every outward sign of handling cleanup correctly, while the pooled array is quietly never returned until garbage collection eventually also collects the leaked rented array, long after this method has already returned.
A struct avoids the heap exactly until the first interface reference touches it – after that, you’re disposing a copy.
