From 0176c1f01837df796b6908961329c4771d0ca5a7 Mon Sep 17 00:00:00 2001 From: Yoshman29 <81191621+YoshiCrafter29@users.noreply.github.com> Date: Wed, 25 Jan 2023 21:03:40 +0100 Subject: [PATCH] FIXED MEMORY LEAK WOOHOO --- buildnumber.txt | 2 +- source/funkin/menus/MainMenuState.hx | 6 +- .../multitasking/MultiTaskingHandler.hx | 38 ----- source/funkin/multitasking/StateWindow.hx | 148 ------------------ source/funkin/system/AssetsLibraryList.hx | 7 - source/funkin/system/Conductor.hx | 18 ++- source/funkin/system/Main.hx | 12 +- source/funkin/system/MusicBeatState.hx | 7 +- .../funkin/system/framerate/AssetTreeInfo.hx | 1 + .../funkin/system/framerate/ConductorInfo.hx | 13 +- .../system/framerate/FramerateCategory.hx | 3 + .../system/framerate/FramerateCounter.hx | 1 + .../funkin/system/framerate/MemoryCounter.hx | 1 + source/funkin/system/framerate/StatsInfo.hx | 9 +- source/funkin/system/framerate/SystemInfo.hx | 11 +- source/funkin/utils/MemoryUtil.hx | 6 +- 16 files changed, 47 insertions(+), 236 deletions(-) delete mode 100644 source/funkin/multitasking/MultiTaskingHandler.hx delete mode 100644 source/funkin/multitasking/StateWindow.hx diff --git a/buildnumber.txt b/buildnumber.txt index 61b5691d..d64b2abb 100644 --- a/buildnumber.txt +++ b/buildnumber.txt @@ -1 +1 @@ -2193 \ No newline at end of file +2199 \ No newline at end of file diff --git a/source/funkin/menus/MainMenuState.hx b/source/funkin/menus/MainMenuState.hx index e039ae6f..10953a17 100644 --- a/source/funkin/menus/MainMenuState.hx +++ b/source/funkin/menus/MainMenuState.hx @@ -103,14 +103,10 @@ class MainMenuState extends MusicBeatState if (!selectedSomethin) { - if (canAccessDebugMenus) { + if (canAccessDebugMenus) if (FlxG.keys.justPressed.SEVEN) FlxG.switchState(new funkin.desktop.DesktopMain()); - if (FlxG.keys.justPressed.F1) - funkin.multitasking.MultiTaskingHandler.openWindow("FreeplayState", new FreeplayState()); - } - if (controls.UP_P) changeItem(-1); diff --git a/source/funkin/multitasking/MultiTaskingHandler.hx b/source/funkin/multitasking/MultiTaskingHandler.hx deleted file mode 100644 index 33331ff3..00000000 --- a/source/funkin/multitasking/MultiTaskingHandler.hx +++ /dev/null @@ -1,38 +0,0 @@ -package funkin.multitasking; - -import flixel.FlxState; -import flixel.FlxG; - -class MultiTaskingHandler { - public static var openedWindows:Array = []; - - public static function init() { - FlxG.signals.postUpdate.add(update); - // FlxG.signals.preDraw.add(draw); - } - - public static function openWindow(name:String, state:MusicBeatState) { - var window = new StateWindow(name, state); - openedWindows.push(window); - } - - public static function update() { - for(window in openedWindows) { - window.update(FlxG.elapsed); - } - } - - public static function draw() { - for(window in openedWindows) { - window.draw(); - } - } - - public static function closeWindow(window:StateWindow) { - openedWindows.remove(window); - - window.window.close(); - window.state.destroy(); - - } -} \ No newline at end of file diff --git a/source/funkin/multitasking/StateWindow.hx b/source/funkin/multitasking/StateWindow.hx deleted file mode 100644 index 82b49fc6..00000000 --- a/source/funkin/multitasking/StateWindow.hx +++ /dev/null @@ -1,148 +0,0 @@ -package funkin.multitasking; - -import flixel.system.frontEnds.BitmapFrontEnd; -import openfl.Lib; -import openfl.display.Sprite; -import flash.events.Event; -import flixel.math.FlxPoint; -import lime.ui.WindowAttributes; -import lime.app.Application; -import lime.ui.Window; -import flixel.FlxState; -import flixel.FlxCamera; -import flixel.FlxG; - -class StateWindow extends Sprite { - public var window:Window; - public var state:MusicBeatState; - public var camera:FlxCamera; - public var cameras:Array; - public var camerasDefault:Array; - public var bmapFrontEnd:BitmapFrontEnd = new BitmapFrontEnd(); - - public function new(windowName:String, state:MusicBeatState) { - super(); - - var windowAttributes:WindowAttributes = { - width: FlxG.width, - height: FlxG.height, - resizable: true - }; - window = Application.current.createWindow(windowAttributes); - window.title = windowName; - window.stage.color = 0x000000; - window.stage.scaleMode = NO_SCALE; - window.stage.align = TOP_LEFT; - window.onClose.add(function() { - MultiTaskingHandler.closeWindow(this); - }); - - window.stage.addChild(this); - - camera = new FlxCamera(); - cameras = [camera]; - camerasDefault = [camera]; - - state.parentWindow = this; - this.state = state; - - beforeStateShit(); - state.create(); - state.createPost(); - afterStateShit(); - - - } - - var oldSize = new FlxPoint(FlxG.width, FlxG.height); - var oldCamList:Array; - var oldCamDefaults:Array; - var oldCam:FlxCamera; - var oldState:FlxState; - var oldFrontEnd:BitmapFrontEnd; - - public function beforeStateShit() { - oldSize.set(FlxG.width, FlxG.height); - - @:privateAccess { - FlxG.width = 1280; - FlxG.height = 720; - - oldCamDefaults = FlxG.cameras.defaults; - oldCamList = FlxG.cameras.list; - oldCam = FlxG.camera; - - FlxCamera._defaultCameras = FlxG.cameras.defaults = camerasDefault; - FlxG.cameras.list = cameras; - FlxG.camera = camera; - - oldState = FlxG.game._state; - FlxG.game._state = state; - - - if (this.numChildren != cameras.length) { - trace("readding cameras..."); - while(this.numChildren > 0) - removeChild(getChildAt(0)); - - for(c in cameras) { - addChild(c.flashSprite); - c.flashSprite.x = c.flashSprite.y = 0; - } - } - - oldFrontEnd = FlxG.bitmap; - FlxG.bitmap = bmapFrontEnd; - } - } - - public function afterStateShit() { - @:privateAccess { - FlxG.width = Std.int(oldSize.x); - FlxG.height = Std.int(oldSize.y); - - camera = FlxG.camera; - cameras = FlxG.cameras.list; - camerasDefault = FlxG.cameras.defaults; - - FlxCamera._defaultCameras = FlxG.cameras.defaults = oldCamDefaults; - FlxG.cameras.list = oldCamList; - FlxG.camera = oldCam; - - oldCamList = null; - oldCam = null; - - FlxG.game._state = oldState; - - FlxG.bitmap = oldFrontEnd; - oldFrontEnd = null; - } - - } - - public function update(elapsed:Float) { - - } - - public function draw() { - - } - - - public override function __enterFrame(t:Float) { - @:privateAccess { - beforeStateShit(); - - FlxG.cameras.update(FlxG.elapsed); - state.tryUpdate(FlxG.elapsed); - - FlxG.cameras.lock(); - state.draw(); - - FlxG.cameras.render(); - FlxG.cameras.unlock(); - - afterStateShit(); - } - } -} \ No newline at end of file diff --git a/source/funkin/system/AssetsLibraryList.hx b/source/funkin/system/AssetsLibraryList.hx index e9d7bde0..66dc6991 100644 --- a/source/funkin/system/AssetsLibraryList.hx +++ b/source/funkin/system/AssetsLibraryList.hx @@ -95,12 +95,9 @@ class AssetsLibraryList extends AssetLibrary { public function getSpecificAsset(id:String, type:String, source:AssetSource = BOTH):Dynamic { try { - MemoryUtil.askDisable(); - if (!id.startsWith("assets/")) { var ass = getSpecificAsset('assets/$id', type, source); if (ass != null) { - MemoryUtil.askEnable(); return ass; } } @@ -109,15 +106,11 @@ class AssetsLibraryList extends AssetLibrary { var asset = e.getAsset(id, type); if (asset != null) { - MemoryUtil.askEnable(); return asset; } } - - MemoryUtil.askEnable(); return null; } catch(e) { - MemoryUtil.askEnable(); throw e; } return null; diff --git a/source/funkin/system/Conductor.hx b/source/funkin/system/Conductor.hx index af85714f..8a514d18 100644 --- a/source/funkin/system/Conductor.hx +++ b/source/funkin/system/Conductor.hx @@ -189,6 +189,8 @@ class Conductor if (FlxG.sound.music == null) reset(); } + private static var __lastChange:BPMChangeEvent; + private static var __updateBeat:Bool; private static function update() { if (FlxG.state != null && FlxG.state is MusicBeatState && cast(FlxG.state, MusicBeatState).cancelConductorUpdate) return; @@ -198,28 +200,28 @@ class Conductor if (bpm > 0) { // updates curbeat and stuff - var lastChange:BPMChangeEvent = { + __lastChange = { stepTime: 0, songTime: 0, bpm: 0 - } + }; for (change in Conductor.bpmChangeMap) { if (Conductor.songPosition >= change.songTime) - lastChange = change; + __lastChange = change; } - if (lastChange.bpm > 0 && bpm != lastChange.bpm) changeBPM(lastChange.bpm); + if (__lastChange.bpm > 0 && bpm != __lastChange.bpm) changeBPM(__lastChange.bpm); - curStepFloat = lastChange.stepTime + ((Conductor.songPosition - lastChange.songTime) / Conductor.stepCrochet); + curStepFloat = __lastChange.stepTime + ((Conductor.songPosition - __lastChange.songTime) / Conductor.stepCrochet); curBeatFloat = curStepFloat / 4; if (curStep != (curStep = Std.int(curStepFloat))) { // updates step - var updateBeat = curBeat != (curBeat = Std.int(curBeatFloat)); + var __updateBeat = curBeat != (curBeat = Std.int(curBeatFloat)); onStepHit.dispatch(curStep); - if (updateBeat) + if (__updateBeat) onBeatHit.dispatch(curBeat); if (FlxG.state is IBeatReceiver) { @@ -228,7 +230,7 @@ class Conductor if (state is IBeatReceiver && (state.subState == null || state.subState.persistentUpdate)) { var st = cast(state, IBeatReceiver); st.stepHit(curStep); - if (updateBeat) + if (__updateBeat) st.beatHit(curBeat); } state = state.subState; diff --git a/source/funkin/system/Main.hx b/source/funkin/system/Main.hx index 8a7d05ab..a3073244 100644 --- a/source/funkin/system/Main.hx +++ b/source/funkin/system/Main.hx @@ -150,9 +150,6 @@ class Main extends Sprite ModsFolder.init(); DesktopMain.init(); DiscordUtil.init(); - #if ALLOW_MULTITASKING - funkin.multitasking.MultiTaskingHandler.init(); - #end #if GLOBAL_SCRIPT funkin.scripting.GlobalScript.init(); #end @@ -242,6 +239,14 @@ class Main extends Sprite // manual asset clearing since base openfl one doesnt clear lime one // doesnt clear bitmaps since flixel fork does it auto + @:privateAccess { + // clear uint8 pools since it causes memory leak with openfl textfield + for(length=>pool in openfl.display3D.utils.UInt8Buff._pools) { + for(b in pool.clear()) + b.destroy(); + } + openfl.display3D.utils.UInt8Buff._pools.clear(); + } scaleMode.resetSize(); var cache = cast(Assets.cache, AssetCache); @@ -252,7 +257,6 @@ class Main extends Sprite Paths.assetsTree.clearCache(); - MemoryUtil.clearMinor(); MemoryUtil.clearMajor(); } } diff --git a/source/funkin/system/MusicBeatState.hx b/source/funkin/system/MusicBeatState.hx index e6fa8733..1bea665f 100644 --- a/source/funkin/system/MusicBeatState.hx +++ b/source/funkin/system/MusicBeatState.hx @@ -14,9 +14,6 @@ import funkin.options.PlayerSettings; class MusicBeatState extends FlxUIState implements IBeatReceiver { - #if ALLOW_MULTITASKING - public var parentWindow:funkin.multitasking.StateWindow; - #end private var lastBeat:Float = 0; private var lastStep:Float = 0; @@ -203,9 +200,7 @@ class MusicBeatState extends FlxUIState implements IBeatReceiver } public override function destroy() { - if (parentWindow == null) - super.destroy(); - + super.destroy(); call("onDestroy"); if (stateScript != null) stateScript.destroy(); diff --git a/source/funkin/system/framerate/AssetTreeInfo.hx b/source/funkin/system/framerate/AssetTreeInfo.hx index eb57f7b2..24aba8f7 100644 --- a/source/funkin/system/framerate/AssetTreeInfo.hx +++ b/source/funkin/system/framerate/AssetTreeInfo.hx @@ -10,6 +10,7 @@ class AssetTreeInfo extends FramerateCategory { } public override function __enterFrame(t:Int) { + if (alpha <= 0.05) return; var text = 'Not initialized yet\n'; if (Paths.assetsTree != null){ text = ""; diff --git a/source/funkin/system/framerate/ConductorInfo.hx b/source/funkin/system/framerate/ConductorInfo.hx index ddbb796e..6d1d6bca 100644 --- a/source/funkin/system/framerate/ConductorInfo.hx +++ b/source/funkin/system/framerate/ConductorInfo.hx @@ -8,13 +8,14 @@ class ConductorInfo extends FramerateCategory { } public override function __enterFrame(t:Int) { - var text = 'Current Song Position: ${Conductor.songPosition}'; - text += '\n - ${Conductor.curBeat} beats'; - text += '\n - ${Conductor.curStep} steps'; - text += '\nCurrent BPM: ${Conductor.bpm}'; - text += '\nCurrent speed: ${FlxMath.roundDecimal(Conductor.speed, 2)}x'; + if (alpha <= 0.05) return; + _text = 'Current Song Position: ${Conductor.songPosition}'; + _text += '\n - ${Conductor.curBeat} beats'; + _text += '\n - ${Conductor.curStep} steps'; + _text += '\nCurrent BPM: ${Conductor.bpm}'; + _text += '\nCurrent speed: ${FlxMath.roundDecimal(Conductor.speed, 2)}x'; - this.text.text = text; + this.text.text = _text; super.__enterFrame(t); } } \ No newline at end of file diff --git a/source/funkin/system/framerate/FramerateCategory.hx b/source/funkin/system/framerate/FramerateCategory.hx index dc88d46e..7e66fb26 100644 --- a/source/funkin/system/framerate/FramerateCategory.hx +++ b/source/funkin/system/framerate/FramerateCategory.hx @@ -11,6 +11,8 @@ class FramerateCategory extends Sprite { public var bgSprite:Sprite; + private var _text:String = ""; + public function new(title:String, text:String = "") { super(); @@ -41,6 +43,7 @@ class FramerateCategory extends Sprite { } public override function __enterFrame(t:Int) { + if (alpha <= 0.05) return; super.__enterFrame(t); var width = Math.max(this.title.width, this.text.width) + (Framerate.instance.x * 2); diff --git a/source/funkin/system/framerate/FramerateCounter.hx b/source/funkin/system/framerate/FramerateCounter.hx index c1d0007c..b6138741 100644 --- a/source/funkin/system/framerate/FramerateCounter.hx +++ b/source/funkin/system/framerate/FramerateCounter.hx @@ -28,6 +28,7 @@ class FramerateCounter extends Sprite { } public override function __enterFrame(t:Int) { + if (alpha <= 0.05) return; super.__enterFrame(t); lastFPS = CoolUtil.fpsLerp(lastFPS, FlxG.elapsed == 0 ? 0 : (1 / FlxG.elapsed), 0.25); diff --git a/source/funkin/system/framerate/MemoryCounter.hx b/source/funkin/system/framerate/MemoryCounter.hx index 43ba5dca..cbb21d5b 100644 --- a/source/funkin/system/framerate/MemoryCounter.hx +++ b/source/funkin/system/framerate/MemoryCounter.hx @@ -31,6 +31,7 @@ class MemoryCounter extends Sprite { } public override function __enterFrame(t:Int) { + if (alpha <= 0.05) return; super.__enterFrame(t); memory = MemoryUtil.currentMemUsage(); diff --git a/source/funkin/system/framerate/StatsInfo.hx b/source/funkin/system/framerate/StatsInfo.hx index 9bd84a7f..e839e448 100644 --- a/source/funkin/system/framerate/StatsInfo.hx +++ b/source/funkin/system/framerate/StatsInfo.hx @@ -10,11 +10,12 @@ class StatsInfo extends FramerateCategory { } public override function __enterFrame(t:Int) { - var text = "totalDC: " + Context3DStats.totalDrawCalls(); - text += "\nstageDC: " + Context3DStats.contextDrawCalls(DrawCallContext.STAGE); - text += "\nstage3DDC: " + Context3DStats.contextDrawCalls(DrawCallContext.STAGE3D); + if (alpha <= 0.05) return; + _text = "totalDC: " + Context3DStats.totalDrawCalls(); + _text += "\nstageDC: " + Context3DStats.contextDrawCalls(DrawCallContext.STAGE); + _text += "\nstage3DDC: " + Context3DStats.contextDrawCalls(DrawCallContext.STAGE3D); - this.text.text = text; + this.text.text = _text; super.__enterFrame(t); } } diff --git a/source/funkin/system/framerate/SystemInfo.hx b/source/funkin/system/framerate/SystemInfo.hx index ef7dc668..172ff1d4 100644 --- a/source/funkin/system/framerate/SystemInfo.hx +++ b/source/funkin/system/framerate/SystemInfo.hx @@ -9,12 +9,13 @@ class SystemInfo extends FramerateCategory { } public override function __enterFrame(t:Int) { - var text = 'System: ${lime.system.System.platformLabel} ${lime.system.System.platformVersion}'; - text += '\nObjs in state: ${FlxG.state.members.length}'; - text += '\nNb cameras: ${FlxG.cameras.list.length}'; - text += '\nCurrent state: ${Type.getClassName(Type.getClass(FlxG.state))}'; + if (alpha <= 0.05) return; + _text = 'System: ${lime.system.System.platformLabel} ${lime.system.System.platformVersion}'; + _text += '\nObjs in state: ${FlxG.state.members.length}'; + _text += '\nNb cameras: ${FlxG.cameras.list.length}'; + _text += '\nCurrent state: ${Type.getClassName(Type.getClass(FlxG.state))}'; - this.text.text = text; + this.text.text = _text; super.__enterFrame(t); } } \ No newline at end of file diff --git a/source/funkin/utils/MemoryUtil.hx b/source/funkin/utils/MemoryUtil.hx index e1a05425..451442f1 100644 --- a/source/funkin/utils/MemoryUtil.hx +++ b/source/funkin/utils/MemoryUtil.hx @@ -29,10 +29,8 @@ class MemoryUtil { enable(); } - public static function init() { - #if cpp - #end - } + public static function init() {} + public static function clearMinor() { #if (cpp || java || neko) Gc.run(false);