From 100f4ede64df349b4dfc07f581cd4bfbbf5c08e6 Mon Sep 17 00:00:00 2001 From: SomeTroglodyte <63000004+SomeTroglodyte@users.noreply.github.com> Date: Fri, 5 May 2023 14:20:30 +0200 Subject: [PATCH] Fix memory leak from repeatedly resetting the font (#9326) * Fix memory leak from repeatedly resetting the font * Reduce ruleset recompilation by MainMenuScreen background changes --- core/src/com/unciv/ui/components/Fonts.kt | 4 ++++ core/src/com/unciv/ui/images/ImageGetter.kt | 2 +- .../screens/mainmenuscreen/MainMenuScreen.kt | 24 +++++++++++-------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/core/src/com/unciv/ui/components/Fonts.kt b/core/src/com/unciv/ui/components/Fonts.kt index dba73282fd..e4582ad228 100644 --- a/core/src/com/unciv/ui/components/Fonts.kt +++ b/core/src/com/unciv/ui/components/Fonts.kt @@ -204,6 +204,10 @@ object Fonts { fun resetFont() { val settings = GUI.getSettings() fontImplementation.setFontFamily(settings.fontFamilyData, settings.getFontSize()) + if (::font.isInitialized) { + (font.data as? NativeBitmapFontData)?.dispose() // See #9325 + // Don't font.dispose() even it it seems obvious -> leaves only black rectangles + } font = fontImplementation.getBitmapFont() font.data.markupEnabled = true } diff --git a/core/src/com/unciv/ui/images/ImageGetter.kt b/core/src/com/unciv/ui/images/ImageGetter.kt index de8e292024..6ceb4a177e 100644 --- a/core/src/com/unciv/ui/images/ImageGetter.kt +++ b/core/src/com/unciv/ui/images/ImageGetter.kt @@ -75,7 +75,7 @@ object ImageGetter { TileSetCache.assembleTileSetConfigs(ruleset.mods) SkinCache.assembleSkinConfigs(ruleset.mods) - Fonts.resetFont() + BaseScreen.setSkin() Fonts.addRulesetImages(ruleset) } diff --git a/core/src/com/unciv/ui/screens/mainmenuscreen/MainMenuScreen.kt b/core/src/com/unciv/ui/screens/mainmenuscreen/MainMenuScreen.kt index 987afda1ea..6cb36ab4e8 100644 --- a/core/src/com/unciv/ui/screens/mainmenuscreen/MainMenuScreen.kt +++ b/core/src/com/unciv/ui/screens/mainmenuscreen/MainMenuScreen.kt @@ -60,6 +60,8 @@ import kotlin.math.min class MainMenuScreen: BaseScreen(), RecreateOnResize { private val backgroundStack = Stack() private val singleColumn = isCrampedPortrait() + + private val backgroundMapRuleset: Ruleset private var easterEggRuleset: Ruleset? = null // Cache it so the next 'egg' can be found in Civilopedia private var backgroundMapGenerationJob: Job? = null @@ -68,7 +70,7 @@ class MainMenuScreen: BaseScreen(), RecreateOnResize { companion object { const val mapFadeTime = 1.3f const val mapFirstFadeTime = 0.3f - const val mapReplaceDelay = 15f + const val mapReplaceDelay = 20f } /** Create one **Main Menu Button** including onClick/key binding @@ -117,7 +119,15 @@ class MainMenuScreen: BaseScreen(), RecreateOnResize { // If we were in a mod, some of the resource images for the background map we're creating // will not exist unless we reset the ruleset and images - ImageGetter.ruleset = RulesetCache.getVanillaRuleset() + val baseRuleset = RulesetCache.getVanillaRuleset() + ImageGetter.ruleset = baseRuleset + + if (game.settings.enableEasterEggs) { + val easterEggMod = EasterEggRulesets.getTodayEasterEggRuleset() + if (easterEggMod != null) + easterEggRuleset = RulesetCache.getComplexRuleset(baseRuleset, listOf(easterEggMod)) + } + backgroundMapRuleset = easterEggRuleset ?: baseRuleset // This is an extreme safeguard - should an invalid settings.tileSet ever make it past the // guard in UncivGame.create, simply omit the background so the user can at least get to options @@ -209,13 +219,7 @@ class MainMenuScreen: BaseScreen(), RecreateOnResize { scale = min(scale, 20f) } - val baseRuleset = RulesetCache.getVanillaRuleset() - easterEggRuleset = EasterEggRulesets.getTodayEasterEggRuleset()?.let { - RulesetCache.getComplexRuleset(baseRuleset, listOf(it)) - } - val mapRuleset = if (game.settings.enableEasterEggs) easterEggRuleset ?: baseRuleset else baseRuleset - - val newMap = MapGenerator(mapRuleset, this) + val newMap = MapGenerator(backgroundMapRuleset, this) .generateMap(MapParameters().apply { shape = MapShape.rectangular mapSize = MapSizeNew(MapSize.Small) @@ -226,7 +230,7 @@ class MainMenuScreen: BaseScreen(), RecreateOnResize { }) launchOnGLThread { // for GL context - ImageGetter.setNewRuleset(mapRuleset) + ImageGetter.setNewRuleset(backgroundMapRuleset) val mapHolder = EditorMapHolder( this@MainMenuScreen, newMap