From d12d3fdcf2a97a94a14061fcef190ca0727f82cf Mon Sep 17 00:00:00 2001 From: Dominic Date: Sun, 2 Oct 2022 14:33:48 -0500 Subject: [PATCH] Fix Fusable Particle memory leaks --- Particles/Metaballs/FusableParticleManager.cs | 46 +++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/Particles/Metaballs/FusableParticleManager.cs b/Particles/Metaballs/FusableParticleManager.cs index 1ac9e5e85b..21a476713f 100644 --- a/Particles/Metaballs/FusableParticleManager.cs +++ b/Particles/Metaballs/FusableParticleManager.cs @@ -23,6 +23,7 @@ namespace CalamityMod.Particles.Metaballs { internal static List ParticleSets = new(); internal static List ExtraModsToLoadSetsFrom = new(); + internal static List ParticleSetTypes = new(); internal static bool HasBeenFormallyDefined = false; /// @@ -51,16 +52,23 @@ namespace CalamityMod.Particles.Metaballs if (!reload) { ParticleSets = new(); + ParticleSetTypes = new(); ExtraModsToLoadSetsFrom = new(); HasBeenFormallyDefined = true; - } - RegisterParticleSetsInMod(CalamityMod.Instance, width, height); - foreach (Mod m in ExtraModsToLoadSetsFrom) - RegisterParticleSetsInMod(m, width, height); + FindParticleSetTypesInMod(CalamityMod.Instance, width, height); + foreach (Mod m in ExtraModsToLoadSetsFrom) + FindParticleSetTypesInMod(m, width, height); + } + else + { + DisposeOfOldRenderTargets(); + foreach (Type t in ParticleSetTypes) + CreateParticleSetOfType(t, width, height); + } } - internal static void RegisterParticleSetsInMod(Mod mod, int width, int height) + internal static void FindParticleSetTypesInMod(Mod mod, int width, int height) { // Look through every type in the mod, and check if it's derived from BaseFusableParticleSet. // If it is, create a default instance of said particle, save it, and create a RenderTarget2D for each individual texture/shader. @@ -71,11 +79,11 @@ namespace CalamityMod.Particles.Metaballs continue; if (type.IsSubclassOf(typeof(BaseFusableParticleSet))) - RegisterParticleSet(type, width, height); + ParticleSetTypes.Add(type); } } - internal static void RegisterParticleSet(Type t, int width, int height) + internal static void CreateParticleSetOfType(Type t, int width, int height) { BaseFusableParticleSet instance = Activator.CreateInstance(t) as BaseFusableParticleSet; List backgroundTargets = new List(); @@ -91,24 +99,34 @@ namespace CalamityMod.Particles.Metaballs ParticleSets.Add(particleRenderCollection); } - internal static void RegisterParticleSet(int width, int height) where T : BaseFusableParticleSet => - RegisterParticleSet(typeof(T), width, height); + internal static void CreateParticleSetOfType(int width, int height) where T : BaseFusableParticleSet => + CreateParticleSetOfType(typeof(T), width, height); + + internal static void DisposeOfOldRenderTargets() + { + // Go through each render collection and manually clear away any render targets. + foreach (FusableParticleRenderCollection particleRenderSet in ParticleSets) + { + foreach (RenderTarget2D target in particleRenderSet.BackgroundTargets) + target?.Dispose(); + } + ParticleSets.RemoveAll(p => p.BackgroundTargets.Any(b => b.IsDisposed)); + } /// /// Unloads all render sets, disposing any created s in the process. /// internal static void UnloadParticleRenderSets() { - // Go through each render collection and manually clear away any render targets. + DisposeOfOldRenderTargets(); + foreach (FusableParticleRenderCollection particleRenderSet in ParticleSets) - { particleRenderSet.ParticleSet = null; - foreach (RenderTarget2D target in particleRenderSet.BackgroundTargets) - target?.Dispose(); - } + HasBeenFormallyDefined = false; ParticleSets = null; ExtraModsToLoadSetsFrom = null; + ParticleSetTypes = null; } ///