[Python-bugs-list] Assigning function objects to instance variable causes memory leak (PR#346)

Tim Peters tim_one@email.msn.com
Mon, 5 Jun 2000 21:30:40 -0400


Assigning a function object is actually harmless.  The problem here is that
you have an instance I, create a bound method object bound to I, then store
the latter as an attribute of I:  this creates a cycle (I.indirectFunc
points to the bound method object, which in turn points back to I).  This is
a well-known case of cyclic trash that CPython's reference counting cannot
reclaim (that is, it's not a bug, in that it's functioning as designed).
Your only hopes for avoiding this behavior-- short of changing your code not
to do this --are to use JPython, or wait for CPython to grow another flavor
of storage reclamation.

> -----Original Message-----
> From: python-bugs-list-admin@python.org
> [mailto:python-bugs-list-admin@python.org]On Behalf Of
> jparkey@rapidbs.com
> Sent: Monday, June 05, 2000 12:07 PM
> To: python-bugs-list@python.org
> Cc: bugs-py@python.org
> Subject: [Python-bugs-list] Assigning function objects to instance
> variable causes memory leak (PR#346)
>
>
> Full_Name: John Parkey
> Version: 1.5.2
> OS: NT (build 132)
> Submission from: mail.rapidbs.com (195.92.50.66)
>
>
> Assigning a function object to an instance variable leaks memory.  Run the
> programme below with the Windows task manager up to see the effect.
>
>
> class fred:
> 	def __init__(self):
> 		self.indirectFunc = self.theFunc
>
> 	def theFunc(self):
> 		return "blah"
>
> def test():
> 	f = fred()
> 	del f
>
>
> if __name__ == "__main__":
> 	for x in xrange(1000):
> 		test()
>
>
>
>
> _______________________________________________
> Python-bugs-list maillist  -  Python-bugs-list@python.org
> http://www.python.org/mailman/listinfo/python-bugs-list
>