Elizabeth Mattijsen said:
> With mixed succes, and with (currently) serious memory leaks.  This  was
> recently discussed on the Perl XML list.  Basic problem is that  Perl
> destruction of SV's is causing parts of the libxml/libxslt
> allocated memory to be freed when it shouldn't yet.

i just spent quite a bit of time working on a solution to exactly this problem.

the C object i was wrapping was reference-counted[1], and the perl wrapper
took a reference on the object.  thing is, i needed the perl object to stick
around as long as the C object, and there were cases when the C object could
still be alive in the library without the perl wrapper.  the trick (not
supplied by me, but by marc lehmann, who knows the black art much better than
i do) was to treat the perl object and the C object as a unit, and "trade"
refcounts between them.  this resulted in the slightly bizarre situation that
DESTROY can be called more than once on a perl object if the C object is being
kept alive by other code.  incidentally, a beautiful part of the solution was
keeping the pointer to the C object in magic associated with the perl object,
invisible and thus unbreakable from prying perl fingers.

if your C object isn't refcounted, this may prove difficult for you.

for a non-refcounted structure[2], my solution was to provide a two-layer
wrapper.  the perl object is an SvRV->SvIV->C wrapper structure->actual object
of interest.  the C wrapper structure is a throwaway struct which contains
useful information like the type of the pointed-to-object, and whether or not
the pointed-to-object should be destroyed with the wrapper.


there are lots of other tricks, but they all are specific to how you actually
will wind up using the structures you're wrapping and how those structures
behave.  what i've told you may be useless directly, but the concepts may be
applicable indirectly.


hope that's mildly helpful.

*grin*


[1] GObject:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/gtk2-perl/gtk2-perl-xs/Glib/GObject.xs?annotate=1.9

[2] GBoxed:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/gtk2-perl/gtk2-perl-xs/Glib/GBoxed.xs?annotate=1.4

-- 
muppet <scott at asofyet dot org>



Reply via email to