# HG changeset patch # User cin # Date 2014-10-07 06:58:38 # Node ID c4877ae77cee283c6d29af0718bab276f09aa91b # Parent 79badb3ed1950cb021161335d0bd3fab40395ff6 ServiceLocator: fixed services cleanup diff --git a/Implab/ServiceLocator.cs b/Implab/ServiceLocator.cs --- a/Implab/ServiceLocator.cs +++ b/Implab/ServiceLocator.cs @@ -20,13 +20,11 @@ namespace Implab { public void Dispose() { if (shared) return; - if (cleanup != null) - cleanup(service); - else { - var d = service as IDisposable; - if (d != null) - d.Dispose(); - } + if (cleanup != null) { + if (service != null) + cleanup(service); + } else + Safe.Dispose(service); } #endregion @@ -173,10 +171,11 @@ namespace Implab { Unregister(typeof(T)); - m_services[typeof(T)] = new ServiceEntry { - activator = () => activator(), - cleanup = instance => cleanup((T)instance) - }; + var serviceEntry = new ServiceEntry(); + serviceEntry.activator = () => activator(); + if (cleanup != null) + serviceEntry.cleanup = instance => cleanup((T)instance); + m_services[typeof(T)] = serviceEntry; } public void Register(Func activator) {