{"id":3871,"date":"2014-03-30T06:09:28","date_gmt":"2014-03-30T06:09:28","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/dramatic-memory-leak-in-wxpython-application-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:09:28","modified_gmt":"2014-03-30T06:09:28","slug":"dramatic-memory-leak-in-wxpython-application-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/dramatic-memory-leak-in-wxpython-application-collection-of-common-programming-errors\/","title":{"rendered":"Dramatic memory leak in wxpython application-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m some way into building a fairly complex wxPython app using ode for physical modelling, openGL for rendering, and wx for UI. Everything was going swimmingly until the application started to crash. After a few days of making no progress I finally noticed that my application was leaking memory. I was able to distill into a smallish example script something that leaks at a quite extraordinary rate:<\/p>\n<pre><code>#-------------------------------------------------------------------------------\n#-------------------------------------------------------------------------------\n\nimport wx\nimport wx.propgrid as wxpg\nimport random\n\nclass CoordProperty(wxpg.PyProperty):\n    def __init__(self, label, name, value=(0,0,0)):\n        wxpg.PyProperty.__init__(self, label, name)\n        self.SetValue(value)\n\n    def GetClassName(self):\n        return \"CoordProperty\"\n\n    def GetEditor(self):\n        return \"TextCtrl\"\n\n    def ValueToString(self, value, flags):\n        x,y,z = value\n        return \"%f,%f,%f\"%(x,y,z)\n\n\napp = wx.App(False)\nframe = wx.Frame(None, -1, \"Test\")\npg = wxpg.PropertyGridManager(frame)\nprops = {}\n\nfor i in range(1000):\n    prop_name = \"prop_%d\"%i\n    prop = CoordProperty(\"Coord\", prop_name)\n    pg.Append(prop)\n    props[prop_name] = prop\n\ndef OnTimer(event):\n    global props\n    for key in props:\n        props[key].SetValue((random.random(), random.random(), random.random()))\n\ntimer = wx.Timer(frame, 1)\nframe.Bind(wx.EVT_TIMER, OnTimer)\ntimer.Start(10) # 100Hz\n\nframe.Show()\n\napp.MainLoop()\ntimer.Stop()\n<\/code><\/pre>\n<p>The example creates a frame, and places a wxPropertyGrid into it. It derives a property that display a 3d co-ordinate value, creates a thousand of them, and then from a timer running at 100Hz it updates each to a random value. This leaks somewhere close to 10Mb\/sec, and eventually crashes. It usually crashes at shutdown too.<\/p>\n<p>I&#8217;m using python 2.7 &amp; wx 2.9.3.1 msw (classic) on Windows 7.<\/p>\n<p>If I replace my derived CoordProperty with a built-in property, such as wxpg.FloatProperty, and modify the code accordingly, the leak goes away.<\/p>\n<p>Any ideas? Or should I submit a wx bug? I can even remove the definition of the function ValueToString in the derived property class and the app still leaks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m some way into building a fairly complex wxPython app using ode for physical modelling, openGL for rendering, and wx for UI. Everything was going swimmingly until the application started to crash. After a few days of making no progress I finally noticed that my application was leaking memory. I was able to distill into [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3871","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3871"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3871\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}