Skip to content

Commit 78c84b9

Browse files
committed
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.
1 parent bdaf950 commit 78c84b9

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

PySimpleGUI27.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,7 @@ def __init__(self, canvas_size, graph_bottom_left, graph_top_right, background_c
20312031
self.DragSubmits = drag_submits
20322032
self.ClickPosition = (None, None)
20332033
self.MouseButtonDown = False
2034-
self.Images = []
2034+
self.Images = {}
20352035
self.RightClickMenu = right_click_menu
20362036

20372037
super().__init__(ELEM_TYPE_GRAPH, background_color=background_color, size=canvas_size, pad=pad, key=key,
@@ -2186,9 +2186,9 @@ def DrawImage(self, filename=None, data=None, location=(None, None), color='blac
21862186
print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***')
21872187
print('Call Window.Finalize() prior to this operation')
21882188
return None
2189-
self.Images.append(image)
21902189
try: # in case closed with X
21912190
id = self._TKCanvas2.create_image(converted_point, image=image, anchor=tk.NW)
2191+
self.Images[id] = image
21922192
except:
21932193
id = None
21942194
return id
@@ -2200,6 +2200,7 @@ def Erase(self):
22002200
print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***')
22012201
print('Call Window.Finalize() prior to this operation')
22022202
return None
2203+
self.Images = {}
22032204
try: # in case window was closed with X
22042205
self._TKCanvas2.delete('all')
22052206
except:
@@ -2208,6 +2209,7 @@ def Erase(self):
22082209

22092210
def DeleteFigure(self, id):
22102211
try:
2212+
del self.Images[id]
22112213
self._TKCanvas2.delete(id)
22122214
except:
22132215
print('DeleteFigure - bad ID {}'.format(id))

0 commit comments

Comments
 (0)