Root Scope keeps references to child scopes forever when attachToParent is set
eValker opened this issue · comments
Hello,
I think I have found a similar issue to #142, but now with scopes :)
If you create a scope using StashboxContainer.BeginScope(attachToParent: true)
and then dispose it,
it will remain referenced in a disposables
field of the root scope anyway.
It is working fine when attachToParent
is false (default) as it is not being registered to the disposables
field.
Sample code:
using Stashbox;
using Stashbox.Configuration;
var container = new StashboxContainer(c =>
{
c.WithRegistrationBehavior(Rules.RegistrationBehavior.PreserveDuplications);
});
RegisterToContainer(container);
while (true)
{
var scope = container.BeginScope(attachToParent: true);
var x = scope.Resolve<IAnimalRepository>();
GC.KeepAlive(x);
scope.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
Console.ReadKey();
static void RegisterToContainer(IStashboxContainer container)
{
container.Register<AnimalRepository>(c => c.WithSingletonLifetime().AsImplementedTypes());
}
internal interface IDataRepository : IDisposable
{
void CommonMethod()
{
}
void IDisposable.Dispose()
{
}
}
internal interface IAnimalRepository : IDataRepository
{
}
internal sealed class AnimalRepository : IAnimalRepository
{
}
Thanks :)
Hey @eValker, thanks for sharing this! While fixing the child container issue, I realized that the same is true for scopes, so I already started to work on the fix. :)
I'll let you know when it's released!
Hi @eValker, I've released 5.12.0-preview-821
with the fix, could you please check that it's working now at your end as expected? Thanks!
@z4kn4fein
I have tested it and it seems to be working fine now :)
Thanks :)
I cannot comment on #141 anymore, but I tested it as well and it seems to be working as expected now :)