diff --git a/compose/ui/ui/src/iosMain/kotlin/androidx/compose/ui/window/MetalRedrawer.ios.kt b/compose/ui/ui/src/iosMain/kotlin/androidx/compose/ui/window/MetalRedrawer.ios.kt index ad73f6ebf05b1..1547468c7e75f 100644 --- a/compose/ui/ui/src/iosMain/kotlin/androidx/compose/ui/window/MetalRedrawer.ios.kt +++ b/compose/ui/ui/src/iosMain/kotlin/androidx/compose/ui/window/MetalRedrawer.ios.kt @@ -278,6 +278,14 @@ internal class MetalRedrawer( caDisplayLink?.invalidate() caDisplayLink = null + // Wait until all scheduled rendering tasks are completed to eliminate race conditions + // when clearing the resources + if (useSeparateRenderThreadWhenPossible) { + trace("MetalRedrawer:dispose:waitForAsyncRenderingTasks") { + dispatch_sync(renderingDispatchQueue) {} + } + } + pictureRecorder.close() context.close() } @@ -431,6 +439,9 @@ internal class MetalRedrawer( picture.close() surface.flushAndSubmit() + surface.close() + renderTarget.close() + if (useSeparateRenderThreadWhenPossible) { dispatch_semaphore_signal(drawCanvasSemaphore) } @@ -470,9 +481,6 @@ internal class MetalRedrawer( } } - surface.close() - renderTarget.close() - // Track current inflight command buffers to synchronously wait for their schedule in case app goes background inflightCommandBuffers.add(commandBuffer)