11

Scenario:

An application instantiates a single instance of a class that implements IDisposable. The instance is exposed through a static member that makes it accessible to all parts of the application. The single instance needs to be kept alive for the lifetime of the application.

Question

How critical is it that the Dispose method be called before the application shuts down and the process terminates?

I've always believed that in most scenarios like this, it is not necessary to call Dispose because the termination of the process naturally cleans up the resources. Am I wrong?

Dan
  • 9,717
  • 4
  • 47
  • 65
  • I am using the exact same pattern with Revit API. Right now, I am unsubscribing to events and disposing off the class on application shut down. Not sure if this is needed. – swiftgp Sep 26 '12 at 04:05

3 Answers3

7

If by "your application shuts down" you mean the process is terminating, then you technically don't need to do anything. Your process is terminating and the OS is going to free up those resources.

I imagine their could be some obscure corner cases where some component could create some type of file or other resource and if Dispose is not called, that won't necessarily be cleaned up, even if your process terminates.

I'd like to give an example of this, albeit this is a bizarre corner case. Let's say you reference and use a component in your code. When you create and use this, it creates a 2GB file on your machine. Now let's take it a step further and say that this component, actually closes the file handle itself accessing this 2GB file sometime during it's use, due to a bug or just bad design. Now the Dispose method on this object cleans up this file, but it is not well-documented. Essentially, missing the Dispose call to this will leave a file on the machine. This is definitely a corner case and won't cause your machine to "leak" anything, but you do have a 2GB file just sitting there.

That being said - the best practice is to call Dispose when you are deterministically done with your resource(s). You could call a method on your Singleton - let's say Cleanup() - that can run when you are shutting down.

Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55
  • I marked this as the answer. My take-away is that the answer is basically: "it depends". I guess the answer depends on why the type implements `IDisposable` and what resources it creates, and what would not be cleaned up without calling the `Dispose` method. Some cases like open file streams are automatically cleaned up by the OS when the process exits. Other cases might involve scenarios that leave garbage behind even after the process terminates if the `Dispose` method isn't called. – Dan Oct 03 '12 at 03:19
  • @usr cleaning file handles and files are two different things. OS also cleans files if `FILE_FLAG_DELETE_ON_CLOSE` flag is set when file is created. – Erdogan Kurtur Oct 04 '12 at 09:41
1

This post has detailed explaination on why disposing of event pub Others is a good practice

.NET object events and dispose / GC

As there are corner cases where gc will not collect Publisher and it will keep firing events even after set to null.

Community
  • 1
  • 1
NiladriBose
  • 1,849
  • 2
  • 16
  • 31
0

If you want to make sure that your Dispose method is called and if it is not a death or alive situation you may attach to AppDomain.CurrentDomain.ProcessExit.

If you absolutely have to be sure that resources are cleaned, then use a shell executable that monitors your main executable, and cleans up after that.

Erdogan Kurtur
  • 3,630
  • 21
  • 39