I've been looking at writing stuff using XML::Genx. Unfortunately, I soon came across a rather nasty little bug. There's an API which lets you pass in a filehandle and it gets all output sent to it. XS has a typemap to help you along with this: T_STDIO. That takes a perl filehandle and gives you a FILE* which you can pass into all the usual C functions.
The trouble is that the typemap doesn't increase the refcount on the perl filehandle. I don't think that it should, because it doesn't know when or where you are likely to need to decrement the refcount.
The net effect is that the filehandle is only valid as long as it's open in Perl. Otherwise it'll get GC'd (and consequently closed) when it goes out of scope. So you have to keep the original filehandle lying around. Surprisingly, I'd managed to do this in all the tests that I had written... Adding a test for this situation provoked a segfault (under Linux, but not FreeBSD for some reason).
Anyway, I'm now faced with two choices.
EndDocument(). And I have nowhere to store the information that I need to do that because my object is a blessed scalar reference.
StartDocFile()and make everything use
StartDocSender()which makes it easier to wrap things up in.
I don't want to deprecate the interface; it's a useful thing to have. But it seems that the alternative is to create a static HV (not visible to Perl) with a key of "$self" and a value of the filehandle SV (or is that really GV?). Then I can check for that in
EndDocument() and decrement it appropriately there. I assume that will work, I have no idea what point Perl runs its GC at.
Four years of pure Perl have really softened me up when it comes to C.