Warning CA2015: Do not define finalizers for types derived from MemoryManager<T>

.NET code analyzer rule CA2015 is enabled, by default, starting in .NET 5. It produces a build warning for any types that derive from MemoryManager<T> that define a finalizer.

Change description

Starting in .NET 5, the .NET SDK includes .NET source code analyzers. Several of these rules are enabled, by default, including CA2015. If your project contains code that violates this rule and is configured to treat warnings as errors, this change could break your build.

Rule CA2015 flags types that derive from MemoryManager<T> that define a finalizer. Adding a finalizer to a type that derives from MemoryManager<T> is likely an indication of a bug. It suggests that a native resource that could have been obtained in a Span<T> is getting cleaned up, potentially while it's still in use by the Span<T>.

Version introduced

5.0

  • Remove the finalizer definition. For more information, see CA2015.

  • To disable code analysis completely, set EnableNETAnalyzers to false in your project file. For more information, see EnableNETAnalyzers.

Affected APIs

Not detectable via API analysis.