On Mon, 8 Nov 2004, Steven N. Hirsch wrote:

On Mon, 8 Nov 2004, Shaun wrote:

I've been staring at this for hours now and I can't work out where I'm leaking memory in a perl XS module I'm working on. Basically, I need to take a tree of C structures:

   typedef struct _SGraph {
       char *name;
       int size;
       Graph list[];
   } * Graph;

with a given head pointer and iterate through the graph and produce Perl hashes which look like { name => "__root__", parent => undef, list = [ { name => "child" }, parent => (ref to parent), list = ... } ]. I've got the algorithm working a number of different ways but each one leaks memory. I'm sending the current implementation along with this post, sorry for the long post, this is the shortest example that demonstrates the issue.

At first glance, it looks like you are setting up circular references. If I'm understanding you correctly, a parent contains a list of references to child nodes, where each child also maintains a reference to its parent. Unless you "weaken" the parent reference (or arrange to forcibly undef it), there's no way Perl will be able to reclaim memory.

Thanks Steve, that was part of the problem, I was also leaking a reference when I did the RvFromRv bit, I didn't need that since the parent owns the child reference (otherwise a dangling reference gets left).


Anyways, thanks heaps for your help, I was close to throwing the computer out the window there.

Cheers,
Shaun

Reply via email to