memory leak fix

the leak started in v0.2.9 (e1ea41f147be2a356d5777e65cce37337cd51555) with the addition of del self.panel[panel].
del cannot be called on self.panels because some panels have bound methods that prevent them from being collected by the gc.
so, instead of trying to remove them, reinitialize the panels if it's needed (before v0.2.9 initialize was always called, so it's still better than it was)
at the moment leaving the panels on memory doesn't seem like a big issue, once all panels are loaded memory should remain stable

fixes #1019
fixes #1008
This commit is contained in:
alfrix 2023-06-18 12:20:37 -03:00
parent 7cf89aa014
commit 2c582acc7b

View File

@ -105,6 +105,7 @@ class KlipperScreen(Gtk.Window):
self.version = version
self.dialogs = []
self.confirm = None
self.panels_reinit = []
configfile = os.path.normpath(os.path.expanduser(args.configfile))
@ -280,18 +281,24 @@ class KlipperScreen(Gtk.Window):
try:
if remove == 2:
self._remove_all_panels()
self.panels_reinit = list(self.panels)
elif remove == 1:
self._remove_current_panel(pop)
if panel_name not in self.panels:
try:
self.panels[panel_name] = self._load_panel(panel_type, self, title, **kwargs)
except Exception as e:
if panel_name in self.panels:
del self.panels[panel_name]
self.show_error_modal(f"Unable to load panel {panel_type}", f"{e}")
return
self._cur_panels.append(panel_name)
if panel_name in self.panels_reinit:
logging.info("Reinitializing panel")
self.panels[panel_name].__init__(self, title, **kwargs)
self.panels_reinit.remove(panel_name)
self.attach_panel(panel_name)
except Exception as e:
logging.exception(f"Error attaching panel:\n{e}")
@ -484,7 +491,6 @@ class KlipperScreen(Gtk.Window):
for panel in list(self.panels):
if hasattr(self.panels[panel], "deactivate"):
self.panels[panel].deactivate()
del self.panels[panel]
self.subscriptions.clear()
self._cur_panels.clear()
self.close_screensaver()