Should factory keep track of created IDisposable objects? - c#

Consider the following simple factory example:
public class MyFactory : IMyFactory
{
public MyObject CreateObject()
{
return new MyObject();
}
}
In this example, MyObject implements the IDisposable interface. Typically I would expect the consuming program to use this as follows:
// Use using to properly dispose of MyObject
using (MyObject obj = myFactory.CreateObject())
{
// ...
}
Is it common practice to expect the developer consuming the Factory to handle disposal like this? Or should the Factory keep a list of objects it has created and make sure they are cleaned up periodically, or, possibly when the factory is disposed?

A type implementing IDisposable provides a way for the consumer of the type to deterministically clean up any unmanaged resources used by the type. E.g., when a FileStream is disposed the underlying OS file handle is closed.
Any properly implemented type that directly uses an unmanaged resource (e.g file handle, database connection, native socket etc.) will also have a finalizer to release the unmanaged resource when the object is garbage collected. It is up to the consumer of the type to either use IDisposable to deterministically release the unmanaged resource or simply wait for the garbage collector to do it. In most cases you want to go the deterministic route. E.g., you do not want to wait for the garbage collector before a file becomes accessible from other processes. You want to close the file as soon as you are done working with that file.
So the garbage collector in combination with finalizers will perform the task that you try to delegate to your factory and because your factory is not the garbage collector you will probably have a very hard time to actually implement this "tracking and automatic clean up".
So my primary answer to your question is, no, the factory should not keep track of created IDisposable objects.
However, if your factory is some sort of container that controls the lifetime of the created objects you can make the factory itself IDisposable and then dispose all created objects on disposal. This is a common pattern in dependency injection containers that are used in response-request cycles like in web applications. The container is created at the start of the request and disposed when the response is complete. The consumers of the types created by the factory are oblivious to the fact the some of the types have to be disposed when the request ends.

"It depends"
When a factory creates IDisposable objects, it often makes sense for the client to dispose of them.
But a common factory pattern is to produce objects which are built using other disposables, but are not themselves disposable. Why would you do that? Because of the viral nature of IDisposable:
If you have a type that is composed out of 5 other inner types, and only the "inner most" is an IDisposable you have a choice:
you can implement the IDisposable pattern on all 5 types so that when you dispose of the "outer most" instance, the one genuine disposable get disposed correctly.
This obviously requires a fair bit of cruft and affects the design of every type in your composition.
or, you can inject the one genuine disposable into your object composition from the factory, and let the factory manage the lifetime of the disposable. In this way the factory starts to fill more of a lifetime management role.
You do see examples of both approach in the real world, but I personally do not like implementing IDisposable just for viral reasons (i.e. my type is creating an IDisposable so must be IDisposable itself - as per .NET Framework Design Guidelines).
WCF's ChannelFactory keeps a list of all the channels it has created and closes them when you Dispose the factory. Similarly many IoC containers (which are themselves essentially super-factories) support a lifetime management role via things like StructureMap's GetNestedContainer() or Autofac's BeginLifetimeScope()

When you implement IDisposable you have to implement yourself the cleaning method for every class. In other words GC won't clean it for you properly leading to a memory leak.
Personally I recommend that every object who implements IDisposable should know how to clean itself.
If your factory creates and returns only instances without keeping list of each one, then each class which administrates external resources or unmanaged resources should implement IDisposable.

Absolutely NOT. Even in real world example, No real factory keep track of its products being trashed after their useful life.
Interesting, I was not assuming some technical discussions to a very simple question. In order to understand we have understand the analogy of the terms:
FACTORY:
A term used to point to the object that is responsible for creating instances of types it can handle.
LIFE TIME MANAGER:
A term used to point to the object that is responsible for the life time of the created instances. It usually work with more than one related instances, such as in the case of Dependency Resolver where it can dispose the dependency object as soon as the dependent object goes out of the scope.
Therefore, it would be better not to tie up so many things within a single class unnecessarily.
I would appreciate the experts opinion on this.

If a factory does not trust that code using the objects it creates will reliably call Dispose upon them, it may be necessary to have the factory keep a link of weak references to such objects along with the information necessary to clean them up. That's an ugly pattern, but in some cases it may be better than any workable alternative if it's important to ensure that abandoned objects get cleaned up. While there are times when it's better to have the produced objects clean themselves up using destructors or Finalize, there are others when it's better to have the factory in charge of such cleanup. For example, if a factory's products subscribe to logical events from the factory, normal event subscriptions would keep those objects from ever getting finalized during the lifetime of the factory. If instead the factory keeps a list of WeakReference to such objects, each notification can cause all live objects to have their notification method invoked, and all dead objects to have their WeakReference removed from the list.

Related

Disposing MemoryCache in Finalizer throws AccessViolationException

EDIT
See edit note at the bottom of the question for additional detail.
Original question
I have a CacheWrapper class which creates and holds onto an instance of the .NET MemoryCache class internally.
MemoryCache hooks itself into AppDomain events, so it will never be garbage-collected unless it is explicitly disposed. You can verify this with the following code:
Func<bool, WeakReference> create = disposed => {
var cache = new MemoryCache("my cache");
if (disposed) { cache.Dispose(); }
return new WeakReference(cache);
};
// with false, we loop forever. With true, we exit
var weakCache = create(false);
while (weakCache.IsAlive)
{
"Still waiting...".Dump();
Thread.Sleep(1000);
GC.Collect();
GC.WaitForPendingFinalizers();
}
"Cleaned up!".Dump();
Because of the behavior, I believe that my MemoryCache instance should be treated as an unmanaged resource. In other words, I should ensure that it is disposed in the finalizer of CacheWrapper (CacheWrapper is itself Disposable follows the standard Dispose(bool) pattern).
However, I am finding that this causes issues when my code runs as part of an ASP.NET app. When the application domain is unloaded, the finalizer runs on my CacheWrapper class. This in turn attempts to dispose the MemoryCache instance. This is where I run into issues. It seems that Dispose attempts to load some configuration information from IIS, which fails (presumably because I'm in the midst of unloading the app domain, but I'm not sure. Here's the stack dump I have:
MANAGED_STACK:
SP IP Function
000000298835E6D0 0000000000000001 System_Web!System.Web.Hosting.UnsafeIISMethods.MgdGetSiteNameFromId(IntPtr, UInt32, IntPtr ByRef, Int32 ByRef)+0x2
000000298835E7B0 000007F7C56C7F2F System_Web!System.Web.Configuration.ProcessHostConfigUtils.GetSiteNameFromId(UInt32)+0x7f
000000298835E810 000007F7C56DCB68 System_Web!System.Web.Configuration.ProcessHostMapPath.MapPathCaching(System.String, System.Web.VirtualPath)+0x2a8
000000298835E8C0 000007F7C5B9FD52 System_Web!System.Web.Hosting.HostingEnvironment.MapPathActual(System.Web.VirtualPath, Boolean)+0x142
000000298835E940 000007F7C5B9FABB System_Web!System.Web.CachedPathData.GetPhysicalPath(System.Web.VirtualPath)+0x2b
000000298835E9A0 000007F7C5B99E9E System_Web!System.Web.CachedPathData.GetConfigPathData(System.String)+0x2ce
000000298835EB00 000007F7C5B99E19 System_Web!System.Web.CachedPathData.GetConfigPathData(System.String)+0x249
000000298835EC60 000007F7C5BB008D System_Web!System.Web.Configuration.HttpConfigurationSystem.GetApplicationSection(System.String)+0x1d
000000298835EC90 000007F7C5BAFDD6 System_Configuration!System.Configuration.ConfigurationManager.GetSection(System.String)+0x56
000000298835ECC0 000007F7C63A11AE System_Runtime_Caching!Unknown+0x3e
000000298835ED20 000007F7C63A1115 System_Runtime_Caching!Unknown+0x75
000000298835ED60 000007F7C639C3C5 System_Runtime_Caching!Unknown+0xe5
000000298835EDD0 000007F7C7628D86 System_Runtime_Caching!Unknown+0x86
// my code here
Is there any known solution to this? Am I correct in thinking that I do need to dispose the MemoryCache in the finalizer?
EDIT
This article validates Dan Bryant's answer and discusses many of the interesting details. In particular, he covers the case of StreamWriter, which faces a similar scenario to mine because it wants to flush it's buffers upon disposal. Here's what the article says:
Generally speaking, finalizers may not access managed objects.
However, support for shutdown logic is necessary for
reasonably-complex software. The Windows.Forms namespace handles this
with Application.Exit, which initiates an orderly shutdown. When
designing library components, it is helpful to have a way of
supporting shutdown logic integrated with the existing
logically-similar IDisposable (this avoids having to define an
IShutdownable interface without any built-in language support). This
is usually done by supporting orderly shutdown when
IDisposable.Dispose is invoked, and an abortive shutdown when it is
not. It would be even better if the finalizer could be used to do an
orderly shutdown whenever possible.
Microsoft came up against this problem, too. The StreamWriter class
owns a Stream object; StreamWriter.Close will flush its buffers and
then call Stream.Close. However, if a StreamWriter was not closed, its
finalizer cannot flush its buffers. Microsoft "solved" this problem by
not giving StreamWriter a finalizer, hoping that programmers will
notice the missing data and deduce their error. This is a perfect
example of the need for shutdown logic.
All that said, I think that it should be possible to implement "managed finalization" using WeakReference. Basically, have your class register a WeakReference to itself and a finalize action with some queue when the object is created. The queue is then monitored by a background thread or timer which calls the appropriate action when it's paired WeakReference gets collected. Of course, you'd have to be careful that your finalize action doesn't inadvertantly hold onto the class itself, thus preventing collection altogether!
You can't Dispose managed objects in the finalizer, as they might have already been finalized (or, as you've seen here, portions of the environment may no longer be in the state you're expecting.) This means that if you contain a class which must be Disposed explicitly, your class must also be Disposed explicitly. There's no way to 'cheat' and make the Disposal automatic. Unfortunately, garbage collection is, in cases like this, a leaky abstraction.
I would suggest that objects with finalizers should generally not be exposed to the outside world, and should only hold strong references to things which are actually needed for finalization and are not exposed to anything in the outside world that is not expecting them to be used for that purpose. Public-facing types should not have finalizers themselves, but should instead encapsulate cleanup logic within privately-held instances of finalizable classes whose purpose is to encapsulate such logic.
The only time it really makes sense for a finalizer to attempt to clean up a resource which is owned by another object is when the other object is designed to interface with the finalizer. I can't think of any places where Framework classes have engineered in the proper hooks, but will offer an example how Microsoft could have engineered them to do so.
A File object could offer an event with thread-safe subscribe and unsubscribe methods which would fire (notifying the last subscriber first) when the File object receives either a Dispose call or a finalize request. The event would fire between the time Finalize is called and the time the encapsulated file is actually closed, and could be used by a an external buffering class as a signal that it needs to give the File any information which it has received but not yet passed along.
Note that to make such a thing work properly and safely, it would be necessary that the part of the File object which has the finalizer not be exposed to the public, and that it use a long weak reference to ensure that if it runs while the public-facing object is still alive, it will reregister itself for finalization. Note that if the only reference to a WeakReference object is stored in a finalizable object, its Target property may be invalidated if the finalizable object becomes eligible for finalization even if the actual target of the reference is still alive. Defective design, IMHO, and one which must be worked around somewhat carefully.
It's possible to design objects with finalizers that can cooperate (the easiest way to do it, generally, is to only have one object in the group sport a finalizer), but if things aren't designed to cooperate with finalizers, the best that one can do is generally have a finalizer sound an alarm indicating "This object wasn't supposed to be Disposed but wasn't; because it wasn't, resources are going to leak, and there's nothing to be done about it except fix the code to dispose the object properly in future".

Disposing the AmazonS3Client and S3Response

I've looked at a number of examples of how to use the AmazonS3Client and S3Response objects and never have I seen anyone bothering to dispose of them, which makes me wonder is there some implied knowledge that I am missing?
public class S3Response : IDisposable
{
public void Dispose();
~S3Response();
}
public class AmazonS3Client : AmazonS3, IDisposable
{
public void Dispose();
~AmazonS3Client();
}
They both clearly implement IDisposable (which is telling me I should be disposing them myself) but they also specify a destructor method, which (along with the aforementioned examples) is making me think have I missed something automagical?
Could it be that the destructor is calling Dispose behind the scenes? Surely it is bad form to perform this kind of magic behaviour.
Does anyone with more experience of the Amazon S3 service have any insight to offer?
First, Destructors are called automatically by the C# Garbage Collector when the object is marked as eligible for destruction, which then calls Finalize. Keep in mind, it could be a long time before the GC runs and decides to do this, and you don't have any real control over it besides manually calling the GC which is not recommended.
Most tutorials only show very basic usage of the libraries, you should definitely be disposing these objects yourself though. (or any object that implements IDisposable)
You of course could do it in a using statement
using(var client = new AmazonS3Client())
{
// use the client here in the using scope
}
// the Dispose() is called after you leave scope of using statement
However, in general some objects are expensive to create (and destroy) and are more meant to be re-used for an extended period of time for several requests. In this case (and probably for the S3Client) you would keep and re-use the same reference to the S3Client for a longer duration then just one request. Keep in mind every time you instantiate the S3Client, it is probably authenticating with Amazon which is time-consuming and expensive.
Say you have a website using the S3Client. You probably want to re-use the same S3Client over the entirety of a web request, or even several web requests. You can achieve this by a Singleton pattern or even a dependency injector library like Unity which you can define an object Lifetime Manager which.

Should I dispose all disposable objects?

Many objects in .NET (SQLCommand for instance) implement IDisposable. As a general rule, if I create an instance of an IDisposable object, should I always dispose it?
Yes, unless you're receiving it from a caller who needs it once you're done or has taken responsibility for calling Dispose(). The important thing is that someone calls Dispose() and if you are being passed an IDisposable instance, there needs to be an understanding ("contract") about whether you are taking ownership for it (and thus need to dispose it) or whether you are "borrowing it" and the caller will use/dispose it upon your return. These are the types of things good APIs have in their documentation.
If you are instantiating the object, make it easy on yourself to automatically dispose by using using.
Make sure to Dispose the items before they lose scope.
The best way is to use using statement, or you can manually call Dispose, but before it loses scope and becomes eligible for garbage collection.
You may see: CA2000: Dispose objects before losing scope
If a disposable object is not explicitly disposed before all
references to it are out of scope, the object will be disposed at some
indeterminate time when the garbage collector runs the finalizer of
the object. Because an exceptional event might occur that will prevent
the finalizer of the object from running, the object should be
explicitly disposed instead.
As a general rule, yes. The easiest way to do this is usually with the using statement.
Almost always yes, but here is an example of when disposing something borrowed can cause problems. I created a disposable repository with two constructors, one that takes a DbContext, and another default constructor that creates the DbContext itself. I ran into instances when my repository would be disposed, triggering the disposal of the DbContext (because I told it to), and that would sometimes cause problems because I still needed the DbContext that had been passed in elsewhere in the code.
In this case, the repository creates the DbContext and must take responsibility for it and dispose of it because no other code can, but when the DbContext is passed in by some other code then disposing of the DbContext should be the responsibility of the code that created it.
Not mandatory in all scenarios, but its a good practice to dispose an object. If you dont want to do it for each object, use using {} on which disposable is implemented.. It will take care of diposing the object when the code block is over.

IDisposable and Destructors in abstract base class

I have an abstract base class which implements IDisposable and the full bool disposed = false, Dispose(), and Dispose(bool) pattern except for the destructor. The base class implements IDisposable since many of its derived classes need to release unmanaged resources. However, I heard that classes with destructors are expensive and thus would make the derived classes that do not have unmanaged resources unnecessarily expensive, had I included the destructor. I'm confused on this matter. Should I or should I not include the destructor and why? Thanks.
You only need to include a destructor/finalizer if you are implementing an entirely new kind of unmanaged resource. So if you're just wrapping or inheriting an existing database connection type, socket type, gdi resource, etc, then you do not need a destructor. The destructor in the original type will take care of finally releasing that resource for you. But if you are implementing something like the ADO.Net provider objects for an entirely new kind of database from scratch, then you would want to implement a destructor for your connection type, so that it can release it's connection when it is finally collected.
Ideally the Dispose pattern relies on the finalizer as well to be complete. The reason is to be sure that the unmanaged resources will be cleaned up. The trick here is that in the Dispose method you also should have the following call: GC.SuppressFinalize(this), which instructs the garbage collector not to treat the instance in a special way, which will keep you from the overhead of finalization. So if the user works with the object correctly disposing it every time (like wrapping in every usage in a using block) then the finalizer won't be called thus not affecting the performance at all.
I heard that classes with destructors are expensive
Classes with finalizers or implementations of IDisposable are not more expensive than those without. However, a class implementing IDisposable is telling the caller that they need to be kept track of and cleaned up when no longer needed. That is additional work for the caller but the cost of not doing so is a resource leak, at least until the class is garbage collected.
In short, if your class does not use any resources that need to be cleaned up, usually in the form of fields that also implement IDisposable, you do not need a finalizer.
The only classes which should override Finalize for the purpose of cleanup are those which either derive directly from Object and expect to clean up their own resources, or things like SafeHandle, whose purpose is to manage resource cleanup. Otherwise, if a derived would have unmanaged resources that need to be cleaned up in Finalize but the base class would not, the proper approach is usually to encapsulate each separate resource in its own finalizable object. Objects which have finalizers should avoid holding strong references to any objects which are not needed for finalization, since all objects to which they hold references, as well as all objects to which any of those object hold references, etc. will be kept alive for an extra garbage-collection generation. If an object George which holds links to many other objects holds a reference to a finalizable object which does not hold a strong back-link, and George is abandoned, the finalizable object will need to be kept around for an extra GC generation but George and the other objects to which it holds direct and indirect references will not. By contrast if George itself implemented Finalize, then it and every object to which it holds a direct or indirect reference would have to be kept around.
Further, finalization can sometimes cause some rare but hard-to-track-down Hindenbugs if the last use of a large finalizable object is actually a use of one of its resources. Code which uses resources that can be cleaned up via Finalize must make sure that the object containing those resources does not become eligible for finalization while those resources are still in use. This is generally done by using GC.KeepAlive(). If a derived class adds a Finalize method that cleans up any resource that existed in the parent class but which the parent class doesn't expect a finalizer to clean up, bugs may occur. Encapsulating the resources in their own class can avoid this problem (it's possible that the parent object might get garbage-collected while the resource is in use, but that won't matter if the encapsulating object is properly designed--the encapsulating object's Finalize method won't run until its methods have finished using the resource).

Singleton with finalizer but not IDisposable

This is what I understand about IDisposable and finalizers from "CLR via C#", "Effective C#" and other resources:
IDisposable is for cleaning up managed and unmanaged resources deterministically.
Classes that are responsible for unmanaged resources (e.g. file handles) should implement IDisposable and provide a finalizer to guarantee that they are cleaned up even if the client code does not call Dispose() on the instance.
Classes that are responsible for managed resources only should never implement a finalizer.
If you have a finalizer then you must implement IDisposable (this allows client code to do the right thing and call Dispose(), while the finalizer prevents leaking resources if they forget).
While I understand the reasoning for and agree with all of the above, there is one scenario where I think it makes sense to break these rules: a singleton class that is responsible for unmanaged resources (such as providing a single point of access to particular files).
I believe it is always wrong to have a Dispose() method on a singleton because the singleton instance should live for the life of the application and if any client code calls Dispose() then you are stuffed. However, you want a finalizer so that when the application is unloaded the finalizer can clean up the unmanaged resources.
So having a singleton class with a finalizer that does not implement IDisposable seems to me to be a reasonable thing to do, yet this type of design is counter to what I understand are best practices.
Is this a reasonable approach? If not, why not and what are the superior alternatives?
I'd first mention that Object Oriented design patterns and their consequences do not always influence every language decision, even in Object Oriented languages. You can certainly find classic design patterns that are easier to implement in one language (Smalltalk) as opposed to another (C++).
That being said, I'm not sure I agree with the premise that a singleton instance should only be disposed at the end of an application. Nothing in the design pattern descriptions that I've read for Singleton (or Design Patterns: Elements of reusable Object-Oriented Software) mention this as a property of this pattern. A singleton should ensure that only one instance of the class exists at any one moment in time; that does not imply that it must exist for as long as the application exists.
I have a feeling that in practice, many singletons do exist for most of the life of an application. However, consider an application that uses a TCP connection to communicate with a server, but can also exist in a disconnected mode. When connected, you would want a singleton to maintain the connection information and state of the connection. Once disconnected, you may want to keep that same singleton - or you may dispose of the singleton. While some may argue that it makes more sense to keep the singleton (and I may even be among them), there is nothing in the design pattern itself that precludes you from disposing of it - if a connection is remade, the singleton can be instantiated again, as no instance of it exists at that moment in time.
In other words, you can create scenarios where it is logical for singletons to have IDisposable.
If the unmanaged resource is released only on application exit you don't even need to worry with a finalizer since the process unload should deal with this for you anyway.
If you have multiple app domains and you want to deal with an app domain unload that's one possible issue but possibly one you don't need to care about.
I second those saying that this design is possibly not the right thing to do (and will make it harder to fix is subsequently you find that you do actually need two instances)
Create the object (or a lazy loading wrapper object) in your entry point and pass it through the code to where it is needed making it clear who is responsible for providing it to whom, then you are free to change the decision to use only one with little effect to the rest of the code (which uses what it gets given)
As long as your finalizer doesn't call methods (such as Dispose) on any other managed objects, you should be fine. Just remember that finalization order is not deterministic. That is, if your singleton object Foo holds a reference to object Bar that requires disposal, you cannot reliably write:
~Foo()
{
Bar.Dispose();
}
The garbage collector might have collected Bar already.
At the risk of stepping into a pile of OO goo (i.e. starting a war), one alternative to using a singleton is to use a static class.
While it may get you code review gripes and FxCop warnings, there's nothing intrinsically wrong with implementing a finalizer without IDisposable. However, doing so on a singleton is not a reliable way to capture process or AppDomain tear-down.
At the risk of offering subjective design advice: If the object is truly stateless, make it a static class. If it is stateful, question why it is a Singleton: you're creating a mutable global variable. If you're trying to capture application close, deal with it when your main loop exits.
Applicability of Singleton to any particular situation aside,
I think there is nothing wrong with Disposing of Singleton. In combination with lazy instantiation it just means that you release resource if you do not need it temporarily, and then re-acquire it as needed.
If you want to create a singleton with a finalizer, you should probably have the static reference to it be a WeakReference. This will require a little bit of extra work to ensure thread-safety in the accessor, but it will allow the singleton to be garbage-collected when nobody is using it (if someone subsequently calls the GetInstance() method, they'll get a new instance). If a static strong reference were used, it would keep the singleton instance alive even if there were no other references to it.

Categories