Instance method decorator garbage collection problem

Christian Heimes lists at cheimes.de
Wed Jun 23 08:51:48 EDT 2010


> The InstanceCounted.count is 1 at the end. If I omit the call to 
> "self.method = print_exception_decorator(self.method)" then the instance 
> count goes down to 0 as desired. I thought that the decorator might be 
> holding a reference to the instance through the bound method, so I added 
>   the __del__() but it doesn't fix the problem.

Adding __del__ introduces a whole bunch of new issues with cyclic gc.
http://docs.python.org/library/gc.html#gc.garbage

> Can anyone suggest anything? Is my technique to decorate bound methods 
> not a good one? How else should I decorate a bound method?

You shouldn't use __del__ to count your instances. Instead you can use
sys.getrefcount() and gc.get_objects() or use a weakref with a callback.

Christian




More information about the Python-list mailing list