Finally Diagnosed a Memory Leak on `EditorHelper`
Lisias opened this issue · 3 comments
For some time, I'm seeing the following message on KSP.log
[WRN 11:04:21.807] DontDestroyOnLoad only works for root GameObjects or components on root GameObjects.
But never managed to connect the dots. Until now, while giving a look on the KSP.log
from a fellow Kerbonaut and found this:
[LOG 15:02:46.685] [KSPCF:MemoryLeaks] A destroyed Scale.PartDB.19x:GameEventEditorVariantAppliedListener instance is owning a onEditorVariantApplied callback. No action has been taken, but unless this mod is relying on this pattern, this is likely a memory leak.
And got puzzled. This helper is intended to be started up only once on Main Menu and to stay there forever:
TweakScale/Source/Scale/EditorHelper.cs
Line 37 in f67bc1b
But then I realized that GameEventEditorListener
is a extended class of SingletonBehavior
, that by itself is a MonoBehaviour
and not a GameObject
. 🤦
TweakScale/Source/Scale/EditorHelper.cs
Line 32 in f67bc1b
Oh, well... Totally my fault. Fix it.
Further analysis revealed that the memory leak wasn't that bad - the GameEventEditorListener
were being instantiated only once, and then never removed from memory.
KSPCF ended up cleaning the thing, and this was what killed the feature when KSPCF is installed.
Yet more interesting... I could new
the Awake
method of a SingletonBehaviour
extended class and everything would just work, but not with OnDestroy
!!!
The extended class' OnDestroy
was never being called, and this was - probably - what prompted me to try the stunt I'm undoing now.
This crap also affected the HotKeyManager
and the PrefabDryCostWriter
!
Oh, yeah... Finally fixed on commit 233510b
[LOG 22:48:31.494] [AddonLoader]: Instantiating addon 'GameEventEditorListener' from assembly 'Scale'
[LOG 22:48:31.495] [TweakScale] TRACE: SingletonBehavior.Awake: TweakScale.GameEventEditorListener
[LOG 22:48:33.906] [TweakScale] TRACE: SingletonBehavior.Start: TweakScale.GameEventEditorListener
[LOG 22:48:37.790] [TweakScale] TRACE: SingletonBehavior.OnDestroy: TweakScale.GameEventEditorListener