From 78c84b9f8fb391470cd7a475f4ad2c0bbafa6ece Mon Sep 17 00:00:00 2001 From: Daniel Jour Date: Mon, 13 May 2019 22:51:23 +0200 Subject: [PATCH] Fix memory leak due to Images of Graph (PySimpleGui27) - Graph used a list self.Images to which new images where appended on DrawImage. Neither in DeleteFigure nor in Erase were any elements removed from that list. Thus any added image was kept in memory as long as the corresponding Graph was; even if it wasn't used anymore. - Even though self.Images is not referred to in any other way, removing the list completely does not work; the result is that no images are drawn on the Graph. - The implemented solution uses a dictionary (id -> image) to keep only used images in self.Images. --- PySimpleGUI27.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/PySimpleGUI27.py b/PySimpleGUI27.py index 6e09fab3..6995f2a3 100644 --- a/PySimpleGUI27.py +++ b/PySimpleGUI27.py @@ -2031,7 +2031,7 @@ class Graph(Element): self.DragSubmits = drag_submits self.ClickPosition = (None, None) self.MouseButtonDown = False - self.Images = [] + self.Images = {} self.RightClickMenu = right_click_menu super().__init__(ELEM_TYPE_GRAPH, background_color=background_color, size=canvas_size, pad=pad, key=key, @@ -2186,9 +2186,9 @@ class Graph(Element): print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***') print('Call Window.Finalize() prior to this operation') return None - self.Images.append(image) try: # in case closed with X id = self._TKCanvas2.create_image(converted_point, image=image, anchor=tk.NW) + self.Images[id] = image except: id = None return id @@ -2200,6 +2200,7 @@ class Graph(Element): print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***') print('Call Window.Finalize() prior to this operation') return None + self.Images = {} try: # in case window was closed with X self._TKCanvas2.delete('all') except: @@ -2208,6 +2209,7 @@ class Graph(Element): def DeleteFigure(self, id): try: + del self.Images[id] self._TKCanvas2.delete(id) except: print('DeleteFigure - bad ID {}'.format(id))