[Python-Dev] Object creation hook

"Martin v. Löwis" martin at v.loewis.de
Sat Jan 27 09:36:06 CET 2007


Kristján V. Jónsson schrieb:
> We have been using gc.get_objects() but it has several problems:
> 1) It returns all objects in the system.

Actually, it doesn't. It only returns objects that participate
in cyclic GC (i.e. container objects).

> 2) There is no way to frame certain operations and get just those
> objects that were created during their execution.  In our case, we
> would like to get the server cluster running, then frame a certain
> operation to get a callback for all created objects, so that we could
> track that they were later destroyed correctly.  I have done this
> previously by storing the id()s of all objects returned from
> gc.get_objects() and comparing them "before" and "after" but this
> suffers from 1) above, and the ambiguity of id() in the face of
> objects being created and destroyed.

As Neal says, people are typically successful in debugging memory
leaks by not looking at the entire list of objects, but looking
first at the number of objects (perhaps by type). It's very easy
to take snapshots of these numbers and then check whether they
grow. Only when you see that the numbers grow in an unreasonable
way, you can try to find out why a specific type has too many
objects created.

> void PyCCP_CreationHookFunc(PyObject * obj) { if (PyCCP_CreationHook)
> { PyObject *result, *tmp = PyCCP_CreationHook; PyCCP_CreationHook =
> 0; //guard against recursion result =
> PyObject_CallFunctionObjArgs(PyCCP_CreationHook, obj, 0); 
> Py_XDECREF(result); if (!result) PyErr_Clear(); PyCCP_CreationHook =
> tmp; } }

I think this is not thread-safe.

Regards,
Martin


More information about the Python-Dev mailing list