From a3a1a8de20f4864b39e198739cae7796a5937174 Mon Sep 17 00:00:00 2001 From: laurids Date: Wed, 17 Apr 2024 23:33:07 +0200 Subject: [PATCH] fix memory leaks --- Assets/GWConquest/Scripts/Battle.cs | 10 ++++++++-- Assets/GWConquest/Scripts/BattleFlank.cs | 10 ++++++++-- Assets/GWConquest/Scripts/District.cs | 13 ++++++++++--- Assets/GWConquest/Scripts/DistrictFactory.cs | 7 ++++++- Assets/GWConquest/Scripts/Formation.cs | 13 ++++++++++--- Assets/GWConquest/Scripts/Planet.cs | 7 ++++++- Assets/GWConquest/Scripts/Player.cs | 10 ++++++++-- Assets/GWConquest/Scripts/Unit.cs | 10 ++++++++-- 8 files changed, 64 insertions(+), 16 deletions(-) diff --git a/Assets/GWConquest/Scripts/Battle.cs b/Assets/GWConquest/Scripts/Battle.cs index 880560e..20ec541 100644 --- a/Assets/GWConquest/Scripts/Battle.cs +++ b/Assets/GWConquest/Scripts/Battle.cs @@ -8,10 +8,10 @@ namespace GWConquest { public class Battle : NetworkBehaviour { - private GWNetworkList formations = new GWNetworkList(); + private GWNetworkList formations; private NetworkVariable zoneID = new NetworkVariable(); private NetworkVariable battleName = new NetworkVariable(); - private GWNetworkList flanks = new GWNetworkList(); + private GWNetworkList flanks; private NetworkVariable flankCount = new NetworkVariable(); private NetworkVariable victorID = new NetworkVariable(); private NetworkVariable phaseCooldown = new NetworkVariable(); @@ -171,6 +171,12 @@ namespace GWConquest base.OnNetworkSpawn(); } + private void Awake() + { + formations = new(); + flanks = new(); + } + private BattleFlank InstantiateNewFlank(int maxUnitCount, int rowCount) { GameObject go = Instantiate(BattleFlankPrefab); diff --git a/Assets/GWConquest/Scripts/BattleFlank.cs b/Assets/GWConquest/Scripts/BattleFlank.cs index d9b06b1..a0c1889 100644 --- a/Assets/GWConquest/Scripts/BattleFlank.cs +++ b/Assets/GWConquest/Scripts/BattleFlank.cs @@ -13,8 +13,8 @@ namespace GWConquest private NetworkVariable flankID = new NetworkVariable(); private NetworkVariable maxUnitCount = new NetworkVariable(); private NetworkVariable rowCount = new NetworkVariable(); - private GWNetworkList units = new GWNetworkList(); - private GWNetworkList deathCooldowns = new GWNetworkList(); + private GWNetworkList units; + private GWNetworkList deathCooldowns; private NetworkVariable defenderStartingCols = new(); private NetworkVariable attackerStartingCols = new(); @@ -68,6 +68,12 @@ namespace GWConquest defenderStartingCols.Value = _defenderStartingCols; } + private void Awake() + { + units = new(); + deathCooldowns = new(); + } + public Unit GetUnit(int index) { return units[index].GetBehaviour(); diff --git a/Assets/GWConquest/Scripts/District.cs b/Assets/GWConquest/Scripts/District.cs index 0e3db27..5881b86 100644 --- a/Assets/GWConquest/Scripts/District.cs +++ b/Assets/GWConquest/Scripts/District.cs @@ -13,7 +13,7 @@ namespace GWConquest private NetworkVariable storageCapacity = new NetworkVariable(); private NetworkVariable controllingPlayerID = new NetworkVariable(); - public Inventory Inventory = new Inventory(); + public Inventory Inventory; private NetworkVariable itemProductionCooldown = new NetworkVariable(); private NetworkVariable zoneID = new NetworkVariable(); private NetworkVariable planet = new NetworkVariable(); @@ -21,8 +21,8 @@ namespace GWConquest private NetworkVariable districtType = new NetworkVariable(); private NetworkVariable districtName = new NetworkVariable(); private NetworkVariable relativePosition = new NetworkVariable(); - private GWNetworkList connectedDistricts = new GWNetworkList(); - private GWNetworkList upgrades = new GWNetworkList(); + private GWNetworkList connectedDistricts; + private GWNetworkList upgrades; private NetworkVariable captureCooldown = new NetworkVariable(); public bool DistrictStatic; @@ -98,6 +98,13 @@ namespace GWConquest get => captureCooldown.Value; } + void Awake() + { + connectedDistricts = new(); + upgrades = new(); + Inventory = new(); + } + public override void OnNetworkSpawn() { diff --git a/Assets/GWConquest/Scripts/DistrictFactory.cs b/Assets/GWConquest/Scripts/DistrictFactory.cs index 0636ba1..bada3f1 100644 --- a/Assets/GWConquest/Scripts/DistrictFactory.cs +++ b/Assets/GWConquest/Scripts/DistrictFactory.cs @@ -39,7 +39,7 @@ namespace GWConquest } public class DistrictFactory : NetworkBehaviour { - public GWNetworkList productionQueue = new GWNetworkList(); + public GWNetworkList productionQueue; public bool StartsBroken = false; public string[] SpecialUnits; @@ -83,6 +83,11 @@ namespace GWConquest } } + private void Awake() + { + productionQueue = new(); + } + public IBuildable GetProductionQueueEntry(int i) { if (productionQueue[i].IsUpgrade) diff --git a/Assets/GWConquest/Scripts/Formation.cs b/Assets/GWConquest/Scripts/Formation.cs index 42ddb9d..4cd2cb7 100644 --- a/Assets/GWConquest/Scripts/Formation.cs +++ b/Assets/GWConquest/Scripts/Formation.cs @@ -33,10 +33,10 @@ namespace GWConquest private NetworkVariable isSpace = new NetworkVariable(); private NetworkVariable currentTransition = new NetworkVariable(); private NetworkVariable player = new NetworkVariable(); - private GWNetworkList units = new GWNetworkList(); - private GWNetworkList pathQueue = new GWNetworkList(); + private GWNetworkList units; + private GWNetworkList pathQueue; private NetworkVariable heroUnit = new NetworkVariable(); - private GWNetworkList subFormations = new GWNetworkList(); + private GWNetworkList subFormations; private NetworkVariable formationName = new NetworkVariable(); private NetworkVariable formationNumber = new NetworkVariable(); private NetworkVariable movementTargetFormation = new NetworkVariable(); @@ -270,6 +270,13 @@ namespace GWConquest public IInventory FormationInventory; + private void Awake() + { + units = new(); + pathQueue = new(); + subFormations = new(); + } + public void TryMoveToPlanet(Planet planet) { if (currentZone.zoneType == ZoneType.Space) diff --git a/Assets/GWConquest/Scripts/Planet.cs b/Assets/GWConquest/Scripts/Planet.cs index 64e45ab..93f733a 100644 --- a/Assets/GWConquest/Scripts/Planet.cs +++ b/Assets/GWConquest/Scripts/Planet.cs @@ -35,7 +35,7 @@ namespace GWConquest private NetworkVariable planetNameVar = new NetworkVariable(); private NetworkVariable spaceZoneID = new NetworkVariable(); private NetworkVariable attackZoneID = new NetworkVariable(); - private GWNetworkList connectedPlanets = new GWNetworkList(); + private GWNetworkList connectedPlanets; private NetworkVariable planetPrefab = new NetworkVariable(); private NetworkVariable prefabRotation = new NetworkVariable(); private NetworkVariable prefabScale = new NetworkVariable(); @@ -153,6 +153,11 @@ namespace GWConquest { get => connectedPlanets.Select(r => r.GetBehaviour()); } + + private void Awake() + { + connectedPlanets = new(); + } public override void OnNetworkSpawn() { diff --git a/Assets/GWConquest/Scripts/Player.cs b/Assets/GWConquest/Scripts/Player.cs index 8a8e926..713b601 100644 --- a/Assets/GWConquest/Scripts/Player.cs +++ b/Assets/GWConquest/Scripts/Player.cs @@ -13,8 +13,8 @@ namespace GWConquest private NetworkVariable factionIndex = new NetworkVariable(); private NetworkVariable isAI = new NetworkVariable(); private NetworkVariable credits = new NetworkVariable(); - private GWNetworkList knownPlanetsList = new GWNetworkList(); - private GWNetworkList visiblePlanetsList = new GWNetworkList(); + private GWNetworkList knownPlanetsList; + private GWNetworkList visiblePlanetsList; public static List PlayerList = new List(); @@ -59,6 +59,12 @@ namespace GWConquest } } + private void Awake() + { + knownPlanetsList = new(); + visiblePlanetsList = new(); + } + public override void OnNetworkSpawn() { Debug.Log("Attaching player..."); diff --git a/Assets/GWConquest/Scripts/Unit.cs b/Assets/GWConquest/Scripts/Unit.cs index fd032db..22c01e7 100644 --- a/Assets/GWConquest/Scripts/Unit.cs +++ b/Assets/GWConquest/Scripts/Unit.cs @@ -20,8 +20,8 @@ namespace GWConquest private NetworkVariable battleState = new NetworkVariable(); private NetworkVariable actionCooldown = new NetworkVariable(); private NetworkVariable actionCooldownMax = new NetworkVariable(); - private Inventory inventory = new Inventory(); - private Inventory equipment = new Inventory(); + private Inventory inventory; + private Inventory equipment; private NetworkVariable flankTarget = new NetworkVariable(); private NetworkVariable morale = new NetworkVariable(); private NetworkVariable supplies = new NetworkVariable(); @@ -255,6 +255,12 @@ namespace GWConquest } } + private void Awake() + { + inventory = new(); + equipment = new(); + } + public override void OnNetworkSpawn() { formation.OnValueChanged += OnFormationChanged;