Should disposable objects be disposed before a Windows Service stops?

It depends on what are "resources used by these objects". Dispose method itself won't be called on process exit, but most objects that contain "unmanaged" resources in addition to Dispose also have finalizer. Finalizer will be called on process exit, but it might not be called on process crash. Now, on process exit (or crash) the following happens:

Any resources allocated by the process are freed.

All kernel objects are closed.

Kernel objects are for example file handles, sockets and so on. So even if process crashed and finalizers are not run (or there was no finalizer at all) - things like files or database\network connections will still be closed by OS.

You might have broader definition of unmanaged resource. Unmanaged means not managed by .NET Framework garbage collector. If I for example create a file on disk when creating some object and going to delete this file when this object is disposed - you might say this is also "unmanaged" resource. Such resource is not known to OS and it will not be "cleaned" if I've not implemented finalizer or finalizer was not called because of process crash.

All in all - if object implements IDisposable- dispose it even before process exit. You might not know the intentions of that object developer, whether it has a finalizer or not - so it's better to always explicitly dispose it even before process exit.


@Evk already gave an answer, but it wasn't completely clear for me. After an extensive search through the documentation, I've compiled the following answer with reference to the documentation.

Long Answer:

When a Service stops, it's resources will be freed by the Garbage collector.

So what about the objects that implement IDisposable? Will the unmanaged resources be freed? No. From Dispose Pattern:

The GC was specifically not designed to manage such unmanaged resources, which means that the responsibility for managing unmanaged resources lies in the hands of the developers.

So, what happens to the unmanaged resources? Are they never going to be freed?

There's still a chance

Object declares a virtual method Finalize (also called the finalizer) that is called by the GC before the object’s memory is reclaimed by the GC and can be overridden to release unmanaged resources.

This however has some drawbacks:

  1. The finalizer of an object is called after some undetermined period of time after the GC detects that it is eligible for collection.
  2. The finalizers are run between collections and so the object's memory is not released until the next round of garbage collection.

Although the documentation says that objects implementing IDisposable.Dispose should either override the Finalize method or wrap the managed object in a SafeHandle so that if the consumer forgets to call Dispose, the unmanaged resources are still freed; we could still end up in trouble.

From the Docs, the Finalize method is called only if the derived type overrides it.

So, what if the developer has implemented neither of the 2 (Finalize or SafeHandle) above? Then we have a problem, there's no one to free the unmanged resources (at least the documentation doesn't say).

TLDR

The resources may or may not be freed (depending on circumstances explained above). So dispose all disposable objects (that are not disposed yet) in the Stop method of your service.

Tags:

C#

Dispose