From 28f6cbaea6a309d86d3267a9189cb0035df99860 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 11 Apr 2021 14:32:42 +0200 Subject: [PATCH] Destroy screen once stream is finished The screen receives callbacks from the decoder, fed by the stream. The decoder is run from the stream thread, so waiting for the end of stream is sufficient to avoid possible use-after-destroy. --- app/src/scrcpy.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 6278cfd7..e6ae4f4c 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -430,10 +430,6 @@ scrcpy(const struct scrcpy_options *options) { LOGD("quit..."); end: - if (screen_initialized) { - screen_destroy(&screen); - } - // stop stream and controller so that they don't continue once their socket // is shutdown if (stream_started) { @@ -459,6 +455,13 @@ end: if (stream_started) { stream_join(&stream); } + + // Destroy the screen only after the stream is guaranteed to be finished, + // because otherwise the screen could receive new frames after destruction + if (screen_initialized) { + screen_destroy(&screen); + } + if (controller_started) { controller_join(&controller); }