diff --git a/VContainer/Assets/VContainer/Runtime/Diagnostics/DiagnosticsContext.cs b/VContainer/Assets/VContainer/Runtime/Diagnostics/DiagnosticsContext.cs index 928431ab..30904f7d 100644 --- a/VContainer/Assets/VContainer/Runtime/Diagnostics/DiagnosticsContext.cs +++ b/VContainer/Assets/VContainer/Runtime/Diagnostics/DiagnosticsContext.cs @@ -52,5 +52,13 @@ internal static DiagnosticsInfo FindByRegistration(Registration registration) { return GetDiagnosticsInfos().FirstOrDefault(x => x.ResolveInfo.Registration == registration); } + + public static void RemoveCollector(string name) + { + lock (collectors) + { + collectors.Remove(name); + } + } } } diff --git a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs index 0baca931..03ef7160 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs @@ -56,6 +56,8 @@ void IDisposable.Dispose() [SerializeField] protected List autoInjectGameObjects; + string scopeName; + static readonly Stack GlobalOverrideParents = new Stack(); static readonly Stack GlobalExtraInstallers = new Stack(); static readonly object SyncRoot = new object(); @@ -129,6 +131,10 @@ static LifetimeScope Find(Type type) protected virtual void Awake() { + if (VContainerSettings.DiagnosticsEnabled && string.IsNullOrEmpty(scopeName)) + { + scopeName = $"{name} ({gameObject.GetInstanceID()})"; + } try { Parent = GetRuntimeParent(); @@ -168,6 +174,10 @@ public void DisposeCore() Container?.Dispose(); Container = null; CancelAwake(this); + if (VContainerSettings.DiagnosticsEnabled) + { + DiagnositcsContext.RemoveCollector(scopeName); + } } public void Build() @@ -188,7 +198,7 @@ public void Build() { builder.RegisterBuildCallback(SetContainer); builder.ApplicationOrigin = this; - builder.Diagnostics = VContainerSettings.DiagnosticsEnabled ? DiagnositcsContext.GetCollector(name) : null; + builder.Diagnostics = VContainerSettings.DiagnosticsEnabled ? DiagnositcsContext.GetCollector(scopeName) : null; InstallTo(builder); }); } @@ -197,7 +207,7 @@ public void Build() var builder = new ContainerBuilder { ApplicationOrigin = this, - Diagnostics = VContainerSettings.DiagnosticsEnabled ? DiagnositcsContext.GetCollector(name) : null, + Diagnostics = VContainerSettings.DiagnosticsEnabled ? DiagnositcsContext.GetCollector(scopeName) : null, }; builder.RegisterBuildCallback(SetContainer); InstallTo(builder);