Skip to content

Commit

Permalink
Do not leak python references when using the gobject.property() helper.
Browse files Browse the repository at this point in the history
Since this helper was storing plain references in a long-lived dict, the
refcount for the instances would never drop to zero, and so they would
never get finalized.

https://bugzilla.gnome.org/show_bug.cgi?id=644039
  • Loading branch information
nud authored and John (J5) Palmieri committed Mar 7, 2011
1 parent 618dbb0 commit 7284d2d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
5 changes: 2 additions & 3 deletions gobject/propertyhelper.py
Expand Up @@ -144,7 +144,6 @@ def __init__(self, getter=None, setter=None, type=None, default=None,

self.name = None

self._values = {}
self._exc = None

def __repr__(self):
Expand Down Expand Up @@ -270,10 +269,10 @@ def _get_maximum(self):
#

def _default_setter(self, instance, value):
self._values[instance] = value
setattr(instance, '_property_helper_'+self.name, value)

def _default_getter(self, instance):
return self._values.get(instance, self.default)
return getattr(instance, '_property_helper_'+self.name, self.default)

def _readonly_setter(self, instance, value):
self._exc = TypeError("%s property of %s is read-only" % (
Expand Down
23 changes: 23 additions & 0 deletions tests/test_properties.py
Expand Up @@ -401,5 +401,28 @@ def test_float_min(self):
gobject.property(type=gobject.TYPE_FLOAT, minimum=-1)
gobject.property(type=gobject.TYPE_DOUBLE, minimum=-1)

# Bug 644039
def testReferenceCount(self):
# We can check directly if an object gets finalized, so we will
# observe it indirectly through the refcount of a member object.

# We create our dummy object and store its current refcount
o = object()
rc = sys.getrefcount(o)

# We add our object as a member to our newly created object we
# want to observe. Its refcount is increased by one.
t = PropertyObject(normal="test")
t.o = o
self.assertEquals(sys.getrefcount(o), rc + 1)

# Now we want to ensure we do not leak any references to our
# object with properties. If no ref is leaked, then when deleting
# the local reference to this object, its reference count shoud
# drop to zero, and our dummy object should loose one reference.
del t
self.assertEquals(sys.getrefcount(o), rc)


if __name__ == '__main__':
unittest.main()

0 comments on commit 7284d2d

Please sign in to comment.