Hello fellow monks

after lots of reading (but less understanding) of perlxstut, perlxs, perlguts and Perl XS: garbage-collecting my malloc'd buffer, but I still cannot figure out when to free external allocated memory.

I have an external library, that allocates memory, which i have to free. It returns the pointer in a char* variable, to which I provide an address. So one try of my code looks like this:

void PropertyGet(self, propName) fwyHANDLEobj self char* propName PREINIT: char* pValue; int rc; PPCODE: rc = fwyPropertyGetChar( self, propName, &pValue ); if (rc == 0) { XPUSHs(sv_2mortal(newSVpv(pValue, 0))); # free(pValue); <=== does not work } else { XSRETURN_UNDEF; }
The C function fwyPropertyGetChar allocates memory and return it in pValue. I am supposed to free it. Now tye answered in the mentioned thread:
When you tell XS that you have a "char *" input buffer, it pulls out a pointer to the string value (if any) stored in the scalar you pass in. So that buffer is allocated just like any other scalar string value buffer in Perl and is free()d in the same situations. So you don't need to worry about a memory leak.
Is that true in my case?
Does sv_2mortal(newSVpv(pValue,0) take the pointer and handle it via garbage collection?

If yes, then I do not understand the line in perlguts (different context though)

sv_setpv(sv, dberror_list[dberror]);
that would try to free a constant constant string. Wouldn't it need an strdup then?

On Windows I get "Free to wrong pool 15d2d20 not 80104 at t/07-sample.t line 80" when I uncomment free, but I get this with every pointer that the API gives me, even when it is not converted to Perl variables, e.g pointers hold in a structure that I free in DESTROY.

Any guidelines for freeing external alocated memory in Perl? Thanks in advance.

And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
(Terry Pratchett, Small Gods)


In reply to XS malloc and free by Brutha

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.