Handling Dispose

Developer
Jun 14, 2009 at 2:44 AM

Kzu,

I was looking at finishing Munq tonight.  All I had to finish is the disposal of Disposable objects.  I came to realize that who should be responsible for the disposal is a function of the Scope not the Owner.  In fact, the owner concept can cause problems as someone might Dispose an object with Container scope.

Thus I came to understand that if Scope is container or hierarchy, then the container is responsible for disposal of instance, otherwise the application is responsible.

For you and FUNQ this means

  • you can remove the Owner property and all the code associated with it.
  • you don't have to track objects that implement IDisposable which is an additional gain during Resolve
  • in the Container.Dispose() method you just have to iterate the services.Values and check if it has an instance which only happens if scope is not None.  If the instance is derived from IDisposable, call its Dispose method and set the instance field in the Service key to null.

I think this may reduce memory leaks from Scope == None and Owner==Container or incorrect dispose calls when Scope != None and Owner == External

Matthew

Coordinator
Oct 2, 2010 at 6:32 AM

yes and now :)

The comment in Container.cs says:

		// Disposable components include factory-scoped instances that we don't keep 
// a strong reference to. 

And that's the key. We also track disposables that have no reuse, and which we therefore don't keep track of in the services.values.

Coordinator
Oct 2, 2010 at 6:32 AM

yes and NO, I meant ;)