IDisposable without finalizer in a Singleton scenario

by Tamás Somogyi   Last Updated July 12, 2018 00:05 AM

Is it a right expectation that if a C# class deals with unmanaged resources and implements IDisposable, then it also should implement some kind of finalizer logic?

We have a vendor-supplied utility class that uses some unmanaged resources. We don't have the source code, so don't know too much implementation details, apart from the fact that it implements IDisposable but does not have any finalizer or SafeHandle. So without calling Dispose, it leaks. Simplified sample code:

public class Utility : IDisposable
    //This class has some unmanaged resources
    //Utility function
    public void DoSomething(...) { ... }
    public void Dispose() { ... }
    protected virtual void Dispose(bool disposing) { ... }

In our code we created a facade around it to simplify the usage and also added a singleton as it is used at dozens of places in our complex legacy codebase. Example:

public class UtilityFacade : IDisposable
    protected Utility utility = new Utility();
    //Simplified utility function
    public void DoSomethingGood() { utility.DoSomething(1,2,3); }
    protected bool disposed = false;
    protected virtual void Dispose(bool disposing)
        if (!disposed)
            if (disposing)
            disposed = true;
    public void Dispose()
    public static UtilityFacade Instance { get; } = new UtilityFacade();

So from our applications (web site, web service, console apps) it would be used like this:


The problem is that since the Instance is a static member, it will never be disposed via IDisposable, so the Utility class will not be disposed either, and since it doesn't implement any finalizer, it leaks unmanaged resources at the end of the application process.

Which is the right choice here:

  1. The vendor says we should call Utility.Dispose() when the application finishes. This would require extra coding for us, e.g. hooking up to Application_End for web apps, or adding try{}finally{...} blocks to console apps for deterministic cleanup, or adding a finalizer to the facade class (which would keep the code related to Utility at one place at least).
  2. We request the vendor to implement the finalizer. Since the Utility class is dealing directly with the unmanaged resources, they should add the finalizer logic. According to some articles on the net it is the best practice recommended by Microsoft.

Or any other recommendation?

Tags : c# singleton

Related Questions

Improve design of singleton pattern

Updated November 26, 2017 09:05 AM

Handling a limited resource in rest WEB API 2

Updated June 19, 2018 08:05 AM

C# best/cleanest way to do singletons?

Updated July 10, 2018 08:05 AM

Loading data from JSON file for one time

Updated July 26, 2016 08:02 AM