VContainer
VContainer copied to clipboard
Diagnostics puts Scoped object into Root scope when factory resolves LifetimeScope
Summary
VContainer Diagnostics shows Scoped objects created in a child LifetimeScope under the Root scope. This happens only when the object is registered with a lambda that calls resolver.Resolve<LifetimeScope>().
builder.Register(resolver =>
{
var scope = resolver.Resolve<LifetimeScope>();
return new MyType { Value = scope.GetInstanceID() };
}, Lifetime.Scoped).AsSelf();
Because of that, old child scopes look alive and their objects stay in memory. Memory Profiler snapshots taken after each scene unload still list every old MyType instance, proving they’re kept alive; with Diagnostics turned off, follow-up snapshots are clean, so the leak only appears when Diagnostics is enabled. Without Diagnostics, objects are released.
Steps to reproduce
- Open the attached Unity project.
- Open Root scene.
- Go to Window → VContainer → Diagnostics and keep the window open.
- Press Play.
- Root scope builds, then a child scope loads from the additive scene.
- Click Next – this unloads the old child scope and makes a new one.
- Watch the tree on the left. MyType instances pile up under Root, even after the child scope is destroyed.
Project with reproduced bug
Might be related to https://github.com/hadashiA/VContainer/issues/784?