Memory leak when the, no objects gets disposed in the weakreferencecollection inside ninject, what am I doing wrong or is there a huge error in ninject?
When Kernal gets disposed then all the references gets disposed, from the weakreference collection, but when in this loop, even when there is no reference from code - the memory just explodes.
public class Program
{
private StandardKernel kernel;
private static void Main(string[] args)
{
new Program().Run();
}
public void Run()
{
kernel = new StandardKernel(new NinjectSettings() { LoadExtensions = false });
kernel.Bind<Root>().ToSelf();
kernel.Bind<Foo>().ToSelf().InCallScope();
while (true)
{
Process();
Thread.Sleep(500);
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
public void Process()
{
Root root = kernel.Get<Root>();
Root root2 = kernel.Get<Root>();
}
public class Root
{
private Foo _test;
public Root(Foo foofac)
{
Id = Guid.NewGuid();
_test = foofac;
}
protected Guid Id { get; set; }
}
public class Foo
{
}
}
Update
Ive tried with Named scope and even using a Factory like in the excample: http://www.planetgeek.ch/2012/04/23/future-of-activation-blocks/ but still in a memoryprofiler the weakreference collection is exploding over time, and this is not nice... My current test code is:
public class Program
{
private StandardKernel kernel;
private static void Main(string[] args)
{
new Program().Run();
}
public void Run()
{
kernel = new StandardKernel(new NinjectSettings() { LoadExtensions = false });
kernel.Load<FuncModule>();
kernel.Bind<IRootFactory>().ToFactory();
var scopeParameterName = "scopeRoot";
kernel.Bind<Root>().ToSelf().DefinesNamedScope(scopeParameterName);
kernel.Bind<Foo>().ToSelf().InNamedScope(scopeParameterName);
while (true)
{
Process();
GC.Collect();
GC.WaitForPendingFinalizers();
Thread.Sleep(500);
}
}
public void Process()
{
Root root = kernel.Get<IRootFactory>().CreateRoot();
Root root2 = kernel.Get<IRootFactory>().CreateRoot();
}
public class Root
{
private Foo _test;
public Root(Foo foofac)
{
Id = Guid.NewGuid();
_test = foofac;
}
protected Guid Id { get; set; }
}
public class Foo
{
}
public interface IRootFactory
{
Root CreateRoot();
}
}