Fix memory leak from repeatedly resetting the font (#9326)

* Fix memory leak from repeatedly resetting the font

* Reduce ruleset recompilation by MainMenuScreen background changes
This commit is contained in:
SomeTroglodyte 2023-05-05 14:20:30 +02:00 committed by GitHub
parent 940fa9a551
commit 100f4ede64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 11 deletions

View File

@ -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
}

View File

@ -75,7 +75,7 @@ object ImageGetter {
TileSetCache.assembleTileSetConfigs(ruleset.mods)
SkinCache.assembleSkinConfigs(ruleset.mods)
Fonts.resetFont()
BaseScreen.setSkin()
Fonts.addRulesetImages(ruleset)
}

View File

@ -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