More error handling for out of memory errors

This commit is contained in:
yairm210 2021-11-12 00:19:52 +02:00
parent c45b6c8f7a
commit 57c033ff34
4 changed files with 24 additions and 14 deletions

View File

@ -53,7 +53,7 @@ class LimitOrientationsHelperAndroid(private val activity: Activity) : LimitOrie
GameSettingsPreview() GameSettingsPreview()
} else try { } else try {
GameSaver.json().fromJson(GameSettingsPreview::class.java, settingsFile.reader()) GameSaver.json().fromJson(GameSettingsPreview::class.java, settingsFile.reader())
} catch (ex: java.lang.Exception) { } catch (throwable: Throwable) {
GameSettingsPreview() GameSettingsPreview()
} }
allowPortrait(setting.allowAndroidPortrait) allowPortrait(setting.allowAndroidPortrait)

View File

@ -173,7 +173,7 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
gameIds[count] = GameSaver.getGameIdFromFile(gameFile) gameIds[count] = GameSaver.getGameIdFromFile(gameFile)
gameNames[count] = gameFile.name() gameNames[count] = gameFile.name()
count++ count++
} catch (ex: Exception) { } catch (ex: Throwable) {
//only getGameIdFromFile can throw an exception //only getGameIdFromFile can throw an exception
//nothing will be added to the arrays if it fails //nothing will be added to the arrays if it fails
//just skip one file //just skip one file
@ -287,6 +287,8 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
val inputDataFailIncrease = Data.Builder().putAll(inputData).putInt(FAIL_COUNT, failCount + 1).build() val inputDataFailIncrease = Data.Builder().putAll(inputData).putInt(FAIL_COUNT, failCount + 1).build()
enqueue(applicationContext, 1, inputDataFailIncrease) enqueue(applicationContext, 1, inputDataFailIncrease)
} }
} catch (outOfMemory: OutOfMemoryError){ // no point in trying multiple times if this was an oom error
return Result.failure()
} }
return Result.success() return Result.success()
} }

View File

@ -198,21 +198,31 @@ class MainMenuScreen: CameraStageBaseScreen() {
val loadingPopup = Popup(this) val loadingPopup = Popup(this)
loadingPopup.addGoodSizedLabel("Loading...") loadingPopup.addGoodSizedLabel("Loading...")
loadingPopup.open() loadingPopup.open()
thread { // Load game from file to class on separate thread to avoid ANR... thread {
var savedGame: GameInfo // Load game from file to class on separate thread to avoid ANR...
try { fun outOfMemory() {
savedGame = GameSaver.loadGameByName(autosave)
} catch (outOfMemory: OutOfMemoryError) {
Gdx.app.postRunnable { Gdx.app.postRunnable {
loadingPopup.close() loadingPopup.close()
ToastPopup("Not enough memory on phone to load game!", this) ToastPopup("Not enough memory on phone to load game!", this)
} }
}
var savedGame: GameInfo
try {
savedGame = GameSaver.loadGameByName(autosave)
} catch (oom: OutOfMemoryError) {
outOfMemory()
return@thread return@thread
} catch (ex: Exception) { // silent fail if we can't read the autosave for any reason - try to load the last autosave by turn number first } catch (ex: Exception) { // silent fail if we can't read the autosave for any reason - try to load the last autosave by turn number first
// This can help for situations when the autosave is corrupted // This can help for situations when the autosave is corrupted
try { try {
val autosaves = GameSaver.getSaves().filter { it.name() != autosave && it.name().startsWith(autosave) } val autosaves = GameSaver.getSaves()
savedGame = GameSaver.loadGameFromFile(autosaves.maxByOrNull { it.lastModified() }!!) .filter { it.name() != autosave && it.name().startsWith(autosave) }
savedGame =
GameSaver.loadGameFromFile(autosaves.maxByOrNull { it.lastModified() }!!)
} catch (oom: OutOfMemoryError) { // The autosave could have oom problems as well... smh
outOfMemory()
return@thread
} catch (ex: Exception) { } catch (ex: Exception) {
Gdx.app.postRunnable { Gdx.app.postRunnable {
loadingPopup.close() loadingPopup.close()
@ -226,11 +236,9 @@ class MainMenuScreen: CameraStageBaseScreen() {
try { try {
game.loadGame(savedGame) game.loadGame(savedGame)
dispose() dispose()
} catch (outOfMemory: OutOfMemoryError) { } catch (oom: OutOfMemoryError) {
loadingPopup.close() outOfMemory()
ToastPopup("Not enough memory on phone to load game!", this)
} }
} }
} }
} }

View File

@ -344,7 +344,7 @@ class WorldScreen(val gameInfo: GameInfo, val viewingCiv:CivilizationInfo) : Cam
Gdx.app.postRunnable { createNewWorldScreen(latestGame) } Gdx.app.postRunnable { createNewWorldScreen(latestGame) }
} }
} catch (ex: Exception) { } catch (ex: Throwable) {
Gdx.app.postRunnable { Gdx.app.postRunnable {
val couldntDownloadLatestGame = Popup(this) val couldntDownloadLatestGame = Popup(this)
couldntDownloadLatestGame.addGoodSizedLabel("Couldn't download the latest game state!").row() couldntDownloadLatestGame.addGoodSizedLabel("Couldn't download the latest game state!").row()