kivy/kivy — Issue #390: cyclic reference while garbage collection in triggers?
Author: shyamdsundar
Created: 2012-01-28 08:30:39+00:00
State: open

Hi,

Recently I wanted to see how garbage collection (GC) is handled in kivy, particularly on creating triggers. If I am correct, Python's GC cannot handle cyclic references (while Java's GC can)

In this regard I found one small problem (which could be a non-issue) when an object stores a trigger referring back to the object itself.

Consider the following example:

============================================
class tmpclass(Widget):
def __init__(self, k=0):
self.k = k
super(tmpclass,self).__init__()
self.trigger = Clock.create_trigger(self.tmpfunc) #self is bounded to trigger
def tmpfunc(self):
print 'triggering', self.k
def __del__(self):
print 'Deleting', self.k

l = []
for i in range(100):
l.append(tmpclass(k=i))
l = []
============================================

By right, one would expect the output as "Deleting 0,1,2,3....". But, it does not happen. While looking at it further, I triggered all the triggers as

for i in l:
l.trigger()

before the last statement (l=[]). Now, I can see that the objects being deleted.

What I dont know is whether the Clock has its own mechanism of dealing with garbage collection (or whether I did anything wrong in creating triggers.)

- Shyam