Skip to content

Commit 2fa5bd4

Browse files
committed
Make a bunch of tweaks to try to fix memory errors blocking shutdown
Tweaked the order of some resources getting destroyed, did a bit more manual cleanup of WX resources in `WxApp`. Also shortened the loop sleep time in SnapshotService so that it can react quicker to kill signals.
1 parent f28dc00 commit 2fa5bd4

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

src/gui/systray.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ def CreatePopupMenu(self):
3939
return menu
4040

4141
def exit(self):
42+
self.RemoveIcon()
4243
if callable(self._on_exit):
4344
self._on_exit()
44-
self.RemoveIcon()
4545

4646
def __enter__(self):
4747
return self

src/gui/wx_app.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class WxApp(wx.App):
2020
def __init__(self):
2121
self._log = logging.getLogger(__name__).getChild(self.__class__.__name__ + '.' + str(id(self)))
2222
super().__init__()
23+
self.SetExitOnFrameDelete(False)
2324

2425
def __new__(cls, *args, **kwargs):
2526
if not isinstance(getattr(cls, '_WxApp__instance', None), cls):
@@ -50,13 +51,27 @@ def check_parent_alive():
5051
if not parent or parent.is_running():
5152
return
5253
self._log.info('parent process no longer running. exiting mainloop...')
54+
if self.timer.IsRunning():
55+
self.timer.Stop()
5356
self.ExitMainLoop()
5457

5558
# enable sigterm by regularly returning control back to python
5659
self.timer = wx.Timer(self._top_frame)
5760
self._top_frame.Bind(wx.EVT_TIMER, lambda *_: check_parent_alive(), self.timer)
5861
self.timer.Start(1000)
5962

63+
def Destroy(self):
64+
if self.timer.IsRunning():
65+
self.timer.Stop()
66+
self.timer.Destroy()
67+
if self._top_frame:
68+
self._top_frame.Destroy()
69+
if top := self.GetTopWindow():
70+
top.Destroy()
71+
if top := self.GetMainTopWindow():
72+
top.Destroy()
73+
return super().Destroy()
74+
6075

6176
def spawn_gui(snapshot: SnapshotFile, start_page: Literal['rules', 'settings'] = 'rules'):
6277
top = WxApp()._top_frame

src/main.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import psutil
99
import win32con
1010
import win32gui
11-
11+
import wx
1212
from common import Window, XandY, load_json, local_path, match, single_call
1313
from device import DeviceChangeCallback, DeviceChangeService
1414
from gui import TaskbarIcon, WxApp, about_dialog, radio_menu
@@ -190,14 +190,16 @@ def find_matching_profile(window: Window) -> Optional[dict]:
190190
@single_call
191191
def shutdown(*_):
192192
log.info('begin shutdown process')
193-
app.ExitMainLoop()
194193
monitor_thread.stop()
195194
snapshot_service.stop()
196-
log.debug('destroy WxApp')
197-
app.Destroy()
195+
window_spawn_thread.stop()
198196
log.info('save snapshot before shutting down')
199197
snap.save()
198+
log.debug('destroy WxApp')
199+
app.ExitMainLoop()
200+
app.Destroy()
200201
log.info('end shutdown process')
202+
wx.Exit()
201203

202204

203205
if __name__ == '__main__':
@@ -288,3 +290,4 @@ def shutdown(*_):
288290
log.info('app mainloop closed')
289291
shutdown()
290292
log.debug('fin')
293+
wx.Exit()

src/snapshot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,6 @@ def _runner(self, snapshot: SnapshotFile):
188188

189189
sleep_start = time.time()
190190
while time.time() - sleep_start < settings.get('snapshot_freq', 30):
191-
time.sleep(1)
191+
time.sleep(0.5)
192192
if self._kill_signal.is_set():
193193
return

0 commit comments

Comments
 (0)