nosmo_1 has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a perl extension. The details of what it does isn't relevant, but the fact that it core dumps is. In my .xs file, I have the following
typedef struct { ... char *line ; ... } parse_data ; parse_data pd ;
There is a function that at one point does:
pd.line = strdup(s) ;
What I want to do later is return the string that I have stored, and free up the memory. The following is one example of how I have tried to do it:
void get_line () PPCODE: EXTEND(SP, 1) ; PUSHs (sv_2mortal(newSVpv(pd.line,0))) ; free (pd.line) ; pd.line = 0 ;
The problem is with the call to free. If this is included, perl core dumps. The strack trace looks like this:
emergency_sbrk(blah) morecore(blah) Perl_sv_grow(blah) Perl_sv_catpvn(blah) Perl_pp_concat(blah) Perl_runops_standard(blah)
Moving the call to free to just before where I reassign a new line works, but after the last call, I would end up with a chunk of unfreed memory hanging around. I *thought* that newSVpv allocated the memory to store the string in, and then copied in my string. If it doesn't, and just runs down the pointer I gave it, then it will of course blow up. How can I pass the value out to Perl and not leave allocated memory hanging about? Yours banging head against wall, Nosmo