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

In reply to Help with Perl Extension by nosmo_1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.