Python bindings and GObject derived class destructor
Posted on 2009-09-29 21:12:55 EEST.
Tero found interesting memory issue with Midgard2 Python bindings. The code he presented is very simple:
import _midgard as midgard
while True:
qb = midgard.query_builder ("midgard_host")
Guess, what happens. Memory usage grows up till your machine is frozen. I started to investigate this issue and reproduced problem with different case:
import gobject
class my_memory_test (gobject.GObject):
def __init__ (self):
gobject.Object.__init (self)
while True:
mmt = my_memory_test()
This script kills machine perfectly too.
As Python bindings for Midgard depends on GObject bindings I started to invent own destructor. Finally solution is plain and simple. Instead of derived destructor (the one assigned to tp_dealloc), I added new one:
void
_py_midgard_gobject_destructor (PyObject *self)
{
PyGObject *pygobj = (PyGObject *) self;
if (pygobj->obj && G_IS_OBJECT (pygobj->obj))
g_object_unref (pygobj->obj);
PyObject_GC_Del (self);
}
Now the question is: does derived class destructor inherits parent class' one if explicitly defined as none? Basic debug statements clearly showed that parent's destructor is invoked but why memory usage grew till death?