in reply to Re: Building an object tree
in thread Building an object tree

As long as parents point to children, but not vica versa, when the parent goes away, its children will have their counts decremented and they go away as well.

The problem only comes if children keep track of their parents. For instance consider a directory tree. Each directory uses "." and ".." to keep track of itself and its parent. What this means is that when the last external reference to a directory tree goes away, the tree hangs around because it refers to itself.

If you wish to get around this you will need to be using Perl 5.6.x or higher, and you will need to install the WeakRef module. (OK, there are other ways, but this one is the simplest.) Then weaken all of the links that cause recursion, and no more memory leaks!

Replies are listed 'Best First'.
Re: Re (tilly) 2: Building an object tree
by MeowChow (Vicar) on Feb 26, 2001 at 03:59 UTC
    WeakRef looks great -- I've wanted something like this on several occasions. Unfortunately, it seems to require a patch to the Perl core. Any possibility of a weakening function becoming a standard part of Perl?
      I don't know what the plans are for it. I think that the idea of a module was to give people a chance to play with the API before they locked an interface down forever. OTOH Perl 6 looks like it is heading towards true garbage collection, so WeakRef is probably going to be filling a need no longer felt.