Skip to content

Commit

Permalink
Potential fix for Android crashes on fast plot rebuilds
Browse files Browse the repository at this point in the history
  • Loading branch information
IKupriyanov-HORIS committed Nov 30, 2023
1 parent aa2ca4d commit 20e8a6a
Showing 1 changed file with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,40 @@ internal class PlotViewContainer(
check(childCount == 1) { "Unexpected number of children: $childCount" }
check(getChildAt(0) == plotSvgPanel) { "Unexpected child: should be SvgPanel but was ${getChildAt(0)::class.simpleName}" }

removeAllViews()
plotSvgPanel.dispose()
viewModel?.dispose()
disposed = true

clearContainer()
}

private fun rebuildSvgPanel() {
LOG.print { "rebuildSvgPanel()" }

if (childCount == 1) {
removeAllViews()
plotSvgPanel.dispose()
viewModel?.dispose()
clearContainer()
}

plotSvgPanel = SvgPanel(context)
viewModel = null
addView(plotSvgPanel)
}

/**
* Looks likely Skiko tries to finish rendering tasks in a same frame where the plot was disposed.
* Deferring plot panel disposing to the next frame seems to fix the error.
* See:
* Android: fast plot rebuild can cause a crash (https://github.com/JetBrains/lets-plot-skia/issues/9)
*/
private fun clearContainer() {
removeAllViews()

// Copy for closure
val panel = plotSvgPanel
val vm = viewModel

post {
panel.dispose()
vm?.dispose()
}
}
}

0 comments on commit 20e8a6a

Please sign in to comment.