| Property | Value |
|---|---|
| Package | Philips.CodeAnalysis.MaintainabilityAnalyzers |
| Diagnostic ID | PH2133 |
| Category | RuntimeFailure |
| Analyzer | UnmanagedObjectsNeedDisposingAnalyzer |
| CodeFix | No |
| Severity | Error |
| Enabled By Default | Yes |
Every class that holds fields that reference unmanaged objects, should implement IDisposable. This to ensure the unmanaged objects are freed properly when finalizing the .NET class.
Implement IDisposable and adhere to the dispose guidelines stated on MS Learn
Code that triggers a diagnostic:
public class BadExample {
private IntPtr pointer;
}And the replacement code:
public sealed class GoodExample : IDisposable {
private IntPtr pointer;
public void Dispose() {
Free(pointer);
}
}This analyzer does not offer any special configuration. The general ways of suppressing diagnostics apply.