それが SQL Server Quality (2)

「信頼性」が問われる実装例として,さすらいの .NET プログラマーさんの ".NET GC & Interop クイズ" から引用*1

public class Wrapper : IDisposable {
  IntPtr _handle;
  public Wrapper(IntPtr handle) { this._handle = handle; }
  public ~Wrapper() { this.Dispose(); }
  public void Dispose() {
    GC.SupressFinalize(this);
    if (this._handle != IntPtr.Zero){
      NativeMethod.CloseHandle(this._handle);
      this._handle = IntPtr.Zero;
    }
  }
  public void Work() {
    NativeMethod.WorkOnHandle(this._handle);
  }
}

このクラスはある特定の条件で動作しない。その条件と直し方は?

もうひとつ SafeHandle (id:NyaRuRu:20050317#p1) 絡みでリソースリークを起こす危険性の例を挙げてみます.

IntPtr myFileHandle = NativeMethods.FindFirstFile(fullPath, data);

*1:タイムリーにSafeHandle: A Reliability Case Study [Brian Grunkemeyer]の Race condition with your own finalizer でも取り上げられている問題です