Cause
Defining finalizers for types derived from System.Buffers.MemoryManager
Rule description
Adding a finalizer to a type derived from System.Buffers.MemoryManager is likely an indication of a bug, as it suggests a native resource that could have been handed out in a System.Span is getting cleaned up and potentially while it is still in use by the System.Span.
How to fix violations
To fix the violation, remove the finalizer definition.
class DerivedClass <T> : MemoryManager<T>
{
public override bool Dispose(bool disposing)
{
if (disposing)
{
_handle.Dispose();
}
}
...
// Violation occurs, remove the finalizer to fix the warning.
~DerivedClass() => Dispose(false);
}When to suppress warnings
It is safe to suppress a violation of this rule if the intent is to create a finalizer for debugging or validation purposes.
Suppress a warning
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA2015
// The code that's violating the rule is on this line.
#pragma warning restore CA2015To disable the rule for a file, folder, or project, set its severity to none in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA2015.severity = noneTo disable this entire category of rules, set the severity for the category to none in the configuration file.
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Reliability.severity = noneFor more information, see How to suppress code analysis warnings.