VContainer icon indicating copy to clipboard operation
VContainer copied to clipboard

Diagnostics puts Scoped object into Root scope when factory resolves LifetimeScope

Open imurashka opened this issue 6 months ago • 1 comments

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.

Image

Steps to reproduce

  1. Open the attached Unity project.
  2. Open Root scene.
  3. Go to Window → VContainer → Diagnostics and keep the window open.
  4. Press Play.
  5. Root scope builds, then a child scope loads from the additive scene.
  6. Click Next – this unloads the old child scope and makes a new one.
  7. Watch the tree on the left. MyType instances pile up under Root, even after the child scope is destroyed.

Project with reproduced bug

VContainer_Diagnostics_Bug.zip

imurashka avatar Jul 06 '25 13:07 imurashka

Might be related to https://github.com/hadashiA/VContainer/issues/784?

imurashka avatar Jul 06 '25 13:07 imurashka