From dd193c2f54c046fb67fc4acf20cdc1ece2294fe9 Mon Sep 17 00:00:00 2001 From: raegan Date: Sun, 16 Jun 2024 15:58:44 +0200 Subject: [PATCH] Fix for usage of ListBuffer to be properly released after leaving the scope --- .../InstanceProviders/FindComponentProvider.cs | 12 +++++++----- .../VContainer/Runtime/Unity/LifetimeScope.cs | 14 ++++++++------ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/InstanceProviders/FindComponentProvider.cs b/VContainer/Assets/VContainer/Runtime/Unity/InstanceProviders/FindComponentProvider.cs index 512044d7..364a5fb1 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/InstanceProviders/FindComponentProvider.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/InstanceProviders/FindComponentProvider.cs @@ -40,12 +40,14 @@ public object SpawnInstance(IObjectResolver resolver) } else if (scene.IsValid()) { - var gameObjectBuffer = UnityEngineObjectListBuffer.Get(); - scene.GetRootGameObjects(gameObjectBuffer); - foreach (var gameObject in gameObjectBuffer) + using (UnityEngineObjectListBuffer.Get(out var gameObjectBuffer)) { - component = gameObject.GetComponentInChildren(componentType, true); - if (component != null) break; + scene.GetRootGameObjects(gameObjectBuffer); + foreach (var gameObject in gameObjectBuffer) + { + component = gameObject.GetComponentInChildren(componentType, true); + if (component != null) break; + } } if (component == null) { diff --git a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs index fcb9d57a..3f64aa2d 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs @@ -101,13 +101,15 @@ public static ExtraInstallationScope Enqueue(IInstaller installer) static LifetimeScope Find(Type type, Scene scene) { - var buffer = UnityEngineObjectListBuffer.Get(); - scene.GetRootGameObjects(buffer); - foreach (var gameObject in buffer) + using (UnityEngineObjectListBuffer.Get(out var buffer)) { - var found = gameObject.GetComponentInChildren(type) as LifetimeScope; - if (found != null) - return found; + scene.GetRootGameObjects(buffer); + foreach (var gameObject in buffer) + { + var found = gameObject.GetComponentInChildren(type) as LifetimeScope; + if (found != null) + return found; + } } return null; }