PyAtkObject: Fix internal object instantiation and reference counting From: Daniel Drake The code is currently using PyObject_New to instantiate objects from C, but this does not properly initialize the objects. References: http://mail.python.org/pipermail/python-dev/2002-April/022877.html http://mail.python.org/pipermail/python-list/2005-April/318746.html Right now, _atkobject_get_parent() creates a mostly-uninitialized object. Later on, when _atkobject_dealloc is called, self->dict is some garbage pointer which we run Py_DECREF on --> crash Also, I noticed that _atkobject_dealloc decreases the reference count on the ATK object, so I also fixed up _atkobject_get_parent to increment the reference count. Index: papi-0.0.2/src/papi_atkobject.c =================================================================== --- papi-0.0.2.orig/src/papi_atkobject.c +++ papi-0.0.2/src/papi_atkobject.c @@ -220,14 +220,7 @@ _atkobject_new (PyTypeObject *type, PyOb PyAtkObject *self; debug ("_atkobject_new\n"); - - self = (PyAtkObject *) type->tp_alloc (type, 0); - self->dict = NULL; - self->prop_handlers = NULL; - self->weakrefs = NULL; - self->obj = NULL; - - return (PyObject *) self; + return PyType_GenericNew (type, args, kwds); } /** @@ -398,8 +391,9 @@ _atkobject_get_parent (PyAtkObject *self val = atk_object_get_parent (ATKOBJECT (self)); if (val) { - PyAtkObject *obj = PyObject_New (PyAtkObject, &PyAtkObject_Type); - obj->obj = val; + PyAtkObject *obj = PyAtkObject_Type.tp_new (&PyAtkObject_Type, NULL, + NULL); + obj->obj = g_object_ref (val); return (PyObject *) obj; } Py_RETURN_NONE; @@ -481,7 +475,7 @@ _atkobject_ref_accessible_child (PyAtkOb return NULL; val = atk_object_ref_accessible_child (ATKOBJECT (self), i); - obj = PyObject_New (PyAtkObject, &PyAtkObject_Type); + obj = PyAtkObject_Type.tp_new (&PyAtkObject_Type, NULL, NULL); obj->obj = val; return (PyObject *) obj; }