views:

53

answers:

2

Hi I´m working on a project that uses Enterprice Libraries´s Unity container to resolve dependencies for exception handling, cache, logging and db access but we keep getting a lot of leaked objects into memory.

We are using property injection like this:

[Dependency]
public Database DB
{
  get { return db; }
  set { db = value; }
}
[Dependency]
public ExceptionManager ExceptionMgr
{
  get { return exceptionManager; }
  set { exceptionManager = value; }
}

Some of the object leaked:

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSetti Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionPolicyData
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ReplaceHandlerData
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.WrapHandlerData Microsoft.Practices.EnterpriseLibrary.Common.Configuration.GenericEnumeratorWrapper Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerData Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings
Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheStorageData

Any general advice on handling dependencies with Unity to avoid object leaks?

A: 

Are you disposing the Database instance properly? Like for example (using db = new Database()) { .... } ?

Edgar Sánchez
Database isn't disposable.
Chris Tavares
+1  A: 

All the objects you list are part of the configuration system. How are you initializing your container? Just calling "AddNewExtension()?" If so, it's not really a leak, since those objects represent the configuration you loaded. The configuration source (which is what's holding on to those objects) stays around for the life of the app so that it can watch for, and notify you, of changes in your application.

What tools are you running that are telling you they're leaking? And are the leaks growing, or constant? Some details would help narrow down the behavior from "expected" to "whoops actual bug".

Also, this is more an Enterprise Library question than a Unity one - Unity itself doesn't leak that I know of.

Chris Tavares
So if I initialize the container multiple times the configuration objects for each initialization will stay around for the entire app life?
Victor Antonio Lopez Villamar
You commment helped a lot.
Victor Antonio Lopez Villamar
Basically yes. There are some tricks you can use to mitigate this. For example, create a container and initialize Entlib in it, then use a child container for everything else. Then drop and reinitialize the child container. That would prevent reloading the Entlib config stuff. You could also try explicitly creating the ConfigurationSource object, configuring the container with it, and then disposing it once the container is configured. Not sure it would work or help, though.
Chris Tavares