Memory leak using richTextCtrl and wxPython

101 Views Asked by At

I have a problem with memory leaks for a application using wxPython and wx.richtext.RichTextCtrl. The application resembles this one - a simple digital clock on a window frame. In order to keep the code clean I use a different module to update the "text module" (ModuleClock.py).

Anyone that knows why it behaves like this?

I have also tried:

  • Replacing the richtext with a StaticText-object (wx.StaticText). Result: doesn't consume memory.
  • No call at all to ModuleClock.py. Result: doesn't consume memory.
  • EDIT 1: Using the commands rto.Freeze(), rto.Thaw() and rto.BeginSupressUndo()
  • EDIT 2: Moving the code from ModuleClock.py into Main.py (inside MainWindow.update method)

Main.py

import wx
import wx.richtext # as rt
import ModuleClock         #
from datetime import datetime
import os
import psutil

class MainWindow(wx.Frame):
    counter = 0
    lastcall = datetime.now()

    def __init__(self, parent):
        wx.Frame.__init__(self, parent, size=(600, 500), pos=(0, 0))

        self.panel = wx.Panel(self)
        self.panel.SetBackgroundColour('Black')

        self.clock = wx.richtext.RichTextCtrl(self.panel, wx.ID_ANY, value="", size = (600,150), style=wx.VSCROLL | wx.HSCROLL | wx.NO_BORDER)
        self.clock.SetBackgroundColour('Black')

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.update, self.timer)
        self.timer.Start(100)
        self.Show()

    def update(self, event):
        now = datetime.now()
        self.counter = self.counter + 1

        ModuleClock.update(self.clock)

        # Check memory use by process every 30 seconds
        if (now - self.lastcall).seconds > 30:
            process = psutil.Process(os.getpid())
            print('memory use =', process.memory_info().rss/1000, 'kb')
            self.lastcall = now


app = wx.App(False)
frame = MainWindow(None)
app.MainLoop()

ModuleClock.py

from datetime import datetime


def update(rto):
    now = datetime.now()

    # default is black text
    rto.Clear()
    rto.BeginFontSize(16)
    rto.BeginTextColour("White")
    rto.WriteText(now.strftime("%A %d %B\n"))
    rto.BeginFontSize(36)
    rto.WriteText(now.strftime("%H:%M:%S"))

Command line output

(venv) C:\Users\Danne\PycharmProjects\Leak>python Main.py
memory use = 31920.128 kb
memory use = 32657.408 kb
memory use = 33492.992 kb
memory use = 34144.256 kb
memory use = 34975.744 kb
memory use = 35545.088 kb
memory use = 36376.576 kb
[30 records removed]
memory use = 58028.032 kb
memory use = 58871.808 kb
memory use = 59404.288 kb
memory use = 60268.544 kb
memory use = 60801.024 kb
memory use = 61607.936 kb
1

There are 1 best solutions below

1
Rolf of Saxony On BEST ANSWER

In ModuleClock you are never cancelling the changes to text colour or the font size i.e. for every Begin there is an End
Change ModuleClock to:

from datetime import datetime


def update(rto):
    now = datetime.now()

    # default is black text
    rto.Clear()
    rto.BeginFontSize(16)
    rto.WriteText(now.strftime("%A %d %B\n"))
    rto.EndFontSize()
    rto.BeginFontSize(36)
    rto.WriteText(now.strftime("%H:%M:%S"))
    rto.EndFontSize()

and add the line self.clock.BeginTextColour("White") after the self.clock.SetBackgroundColour('Black') in Main.py