Skip to content

Latest commit

 

History

History
43 lines (32 loc) · 1.43 KB

File metadata and controls

43 lines (32 loc) · 1.43 KB

PH2133: Unmanaged objects need disposing

Property Value
Package Philips.CodeAnalysis.MaintainabilityAnalyzers
Diagnostic ID PH2133
Category RuntimeFailure
Analyzer UnmanagedObjectsNeedDisposingAnalyzer
CodeFix No
Severity Error
Enabled By Default Yes

Introduction

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.

How to solve

Implement IDisposable and adhere to the dispose guidelines stated on MS Learn

Example

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);
  }
}

Configuration

This analyzer does not offer any special configuration. The general ways of suppressing diagnostics apply.