AST structures and memory leaks
Gordon McMillan
gmcm at hypernet.com
Thu Mar 16 09:05:32 EST 2000
Robin Becker wrote:
> I want to have an AST container A contain instances of B and would like
> the instances to know who contains them, but can't seem to avoid memory
> leaks.
>
> The general structure is
>
> class A:
> def __init__(self):
> self.list=[]
> def add(self,b1):
> self.list.append(b)
>
> class B:
> def __init__(self,owner):
> self.owner=owner
>
> a=A()
> a.add(B(a))
>
> which is clearly loopy :)
>
> Is there any way to find the 'owner' of an instance?
The general case would probably be:
class AST:
def __init__(self, owner, kids=None):
self.owner = owner
self.kids = kids
if kids is None:
self.kids = []
def add(self, kid):
self.kids.append(kid)
def cleanup(self):
for kid in self.kids:
kid.cleanup()
self.owner = None
...which obviously requires an explicit call to root.cleanup()
when you're done.
I-suppose-I-could-charge-Andy-for-that-<wink>-ly y'rs
- Gordon
More information about the Python-list
mailing list