Hi Martin,
in short - this is by design.
The factory is a singleton so disposing it will take no effect.
Also Releasing component directly with the container might have worked
in previous version but it was due to a flaw in the design.
The factory 'owns' the object and if it was resolved from the factory,
it ought to be released via the factory.
HTH,
Krzysztof
On 21/12/2011 12:40 AM, Martin Larsen wrote:
Hi
I have found a bug in 3.0.0.2314 of the WindsorContainer. When
releasing an object which is created by a factory from the container
the object is not garbage collected, thereby creating a memory leak.
You can reproduce the bug using the following code:
namespace MemTest
{
class Program
{
private static IWindsorContainer _container;
static void Main(string[] args)
{
_container = new WindsorContainer();
_container.AddFacility<TypedFactoryFacility>();
_container.Register(Component.For<IServiceFactory>().AsFactory());
_container.Register(Component.For<IService>().ImplementedBy<Service>().LifeStyle.Transient);
CreateInstanceByResolve();
GC.Collect();
Console.WriteLine("Done (rosolving)...");
Thread.Sleep(1000);
CreateInstanceByFactory();
GC.Collect();
Console.WriteLine("Done (factory)...");
Thread.Sleep(1000);
_container.Dispose();
GC.Collect();
Console.WriteLine("Done (disposing container)...");
Thread.Sleep(1000);
Console.ReadLine();
}
private static void CreateInstanceByResolve()
{
IService serviceByResolve =
_container.Resolve<IService>();
// This will correctly release the object and garbage
collect the object
_container.Release(serviceByResolve);
}
private static void CreateInstanceByFactory()
{
IServiceFactory factory =
_container.Resolve<IServiceFactory>();
IService serviceByFactory = factory.Create();
// This will NOT release the object and garbage collection
will occur when container is disposed. It does release it in 2.5.3.
_container.Release(factory);
// This will NOT release it either - it worked before?
_container.Release(serviceByFactory);
}
}
public class Service : IService
{
~Service()
{
Console.WriteLine("GC");
}
public void Dispose()
{
}
}
public interface IService : IDisposable
{
}
public interface IServiceFactory
{
IService Create();
}
}
The output of the program is:
Done (rosolving)...
GC
Done (factory)...
Done (disposing container)...
GC
which shows that the object created by the factory is not garbage
collected until the container is disposed. I would have expected the
following (which is the case for 2.5.3):
Done (rosolving)...
GC
Done (factory)...
GC
Done (disposing container)...
--
You received this message because you are subscribed to the Google Groups "Castle
Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en.