Skip to content

Commit bdaf950

Browse files
committed
Fix memory leak due to Images of Graph
- 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 7bfa71a commit bdaf950

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

PySimpleGUI.py

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

20352035
super().__init__(ELEM_TYPE_GRAPH, background_color=background_color, size=canvas_size, pad=pad, key=key,
@@ -2184,9 +2184,9 @@ def DrawImage(self, filename=None, data=None, location=(None, None), color='blac
21842184
print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***')
21852185
print('Call Window.Finalize() prior to this operation')
21862186
return None
2187-
self.Images.append(image)
21882187
try: # in case closed with X
21892188
id = self._TKCanvas2.create_image(converted_point, image=image, anchor=tk.NW)
2189+
self.Images[id] = image
21902190
except:
21912191
id = None
21922192
return id
@@ -2198,6 +2198,7 @@ def Erase(self):
21982198
print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***')
21992199
print('Call Window.Finalize() prior to this operation')
22002200
return None
2201+
self.Images = {}
22012202
try: # in case window was closed with X
22022203
self._TKCanvas2.delete('all')
22032204
except:
@@ -2206,6 +2207,7 @@ def Erase(self):
22062207

22072208
def DeleteFigure(self, id):
22082209
try:
2210+
del self.Images[id]
22092211
self._TKCanvas2.delete(id)
22102212
except:
22112213
print('DeleteFigure - bad ID {}'.format(id))

0 commit comments

Comments
 (0)