From c2a139e47743f54b5a623cafe0fc4aa7b1f26de4 Mon Sep 17 00:00:00 2001 From: Tino Date: Sat, 16 Dec 2023 12:59:53 -0500 Subject: [PATCH] Fix memory leak and slowing down when video output is enabled --- modules/gui/emulator_screen.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/gui/emulator_screen.py b/modules/gui/emulator_screen.py index 7390a97b..1befcaf3 100644 --- a/modules/gui/emulator_screen.py +++ b/modules/gui/emulator_screen.py @@ -15,6 +15,7 @@ def __init__(self, window: Tk): self.frame: Union[ttk.Frame, None] = None self.canvas: Union[Canvas, None] = None self.current_canvas_image: Union[PhotoImage, None] = None + self._current_canvas_image_id: int | None = None self._placeholder_image: Union[PhotoImage, None] = None self.center_of_canvas: tuple[int, int] = (240, 160) @@ -140,10 +141,14 @@ def _generate_placeholder_image(self): self.canvas.create_image(self.center_of_canvas, image=self.current_canvas_image, state="normal") def _update_image(self, image: PIL.Image): + if self._current_canvas_image_id: + self.canvas.delete(self._current_canvas_image_id) self.current_canvas_image = PIL.ImageTk.PhotoImage( image=image.resize((self.width * self.scale, self.height * self.scale), resample=False) ) - self.canvas.create_image(self.center_of_canvas, image=self.current_canvas_image, state="normal") + self._current_canvas_image_id = self.canvas.create_image( + self.center_of_canvas, image=self.current_canvas_image, state="normal" + ) self._update_window() def _update_window(self):