Object leak in Unity

Sep 2, 2009 at 2:40 PM
Edited Sep 2, 2009 at 3:48 PM

Given this scenario:

 



static void Main(string[] args) { var container = new UnityContainer(); CreateObject(container); Console.Read(); } private static void CreateObject(UnityContainer container) { var childContainer = container.CreateChildContainer(); var objToRegister = new ObjectSpace(); childContainer.RegisterInstance<IObjectSpace>(objToRegister); var obj = childContainer.Resolve<IObjectSpace>(); }

 

It seems like the objectToRegister is never released. The parent container holds a reference to the child container which has, correctly, registered the objToRegister with a ContainerControlledLifteTimeManger.

 

The issue however is that since the parent never releases the child, the ContainerControlledLifeTime is not tied into the child but the parent. This behavorp seems very odd. Is there a way for us to make sure that when the child dies, all it's registered instances die with it? Or am I just missing something very basic here?

 

Sep 2, 2009 at 5:43 PM

Your analysis is a little off. The parent container doesn't hold any references to the child. However, what you've got here is the child container has become garbage, and so is at the mercy of the garbage collector, which may or may not get invoked.

The answer here is to:

1) Implement IDisposable on your IObjectSpace implementation.

2) Call Dispose on the child container when you're done ( or put it in a using() block)

When the child container is Disposed, all container controlled instances will be disposed as well.

-Chris

Sep 2, 2009 at 8:00 PM

Well,

Looking at the object graph in memory, the parent container has a reference to the child container in the private member lifeTimeManager (I think that was the name, I don't have visual studio where I'm at atm). It's not about the garbage collector.

Are you saying that there is a weakreference to the child from the parents lifeTimeManager? How exactly would implementing IDiposable on my object solve anything? I don't have any references in my object that needs disposing.

Sep 2, 2009 at 8:54 PM

Disposing the childcontainer does indeed remove the reference from the parent container. But I don't understand why I would want to implement IDisposable on the object itself?

Sep 3, 2009 at 6:51 AM

Ah, right, I'm with you now. I forgot about the lifetime manager reference... I must be getting old or something. :-)

I also misinterpreted what you meant by "release". If all you want to do is get your objects to be garbage collected, then disposing the container is all you need to do. That'll dispose the current container any any children created from it, and delete any references to objects they may hold. In addition, if those objects implement IDisposable, those objects will also be Disposed as well.

So, if all you want it to get GC'ed, then disposing the container is it. If your object has additional cleanup semantics you also want called, implement IDisposable and that'll get called.

Hope this makes a little more sense,

-Chris

Sep 3, 2009 at 7:47 AM
It does yes.

And as I suspected I missed something obvious, almost embarassing ;)

On Thu, Sep 3, 2009 at 7:51 AM, ctavares <notifications@codeplex.com> wrote:

From: ctavares

Ah, right, I'm with you now. I forgot about the lifetime manager reference... I must be getting old or something. :-)

I also misinterpreted what you meant by "release". If all you want to do is get your objects to be garbage collected, then disposing the container is all you need to do. That'll dispose the current container any any children created from it, and delete any references to objects they may hold. In addition, if those objects implement IDisposable, those objects will also be Disposed as well.

So, if all you want it to get GC'ed, then disposing the container is it. If your object has additional cleanup semantics you also want called, implement IDisposable and that'll get called.

Hope this makes a little more sense,

-Chris

Read the full discussion online.

To add a post to this discussion, reply to this email (unity@discussions.codeplex.com)

To start a new discussion for this project, email unity@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Sep 3, 2009 at 7:48 AM

Yeah as I stated earlier disposing the child container and as I suspected in my first post, it was indeed something very basic I was missing. I must getting old as well. Missing Dispose, almost embarrasing ;)