Free memory after newSVpv

[email protected] (Bill Moseley)
Newsgroups perl.xs
Message-ID <CAKhN_m4HmPY+=jZwKHtmUp4YCaSwNCPZQ-4=8AMwv82TEEkW_A@mail.gmail.com>
Very quick question I hope someone can answer.

I'm calling sendTask() in a C++ library where I pass a char* that is
supposedly malloced and returned so I need to free() it.

The problem is when I call Safefree()  below the code fails (in odd ways),
but if I don't call Safefree() /free() then it works ok.


Looking at the code below, if "buffer" is malloc'd and returned and newSVpv
*copies* the buffer into the SV then is it correct to call Safefree() or
free() as I'm doing below?


void
sendTask( MyClient * THIS, unsigned int id, char * message_payload,
unsigned int message_length )
    PPCODE:
        char * buffer = NULL;
        int  buffer_length;
        int  status;

        THIS->sendTask( id, message_payload, message_length, buffer,
&buffer_length, &status );
        XPUSHs( sv_2mortal( newSVpv( buffer, buffer_length ) ) );
       * Safefree( buffer );*
        XPUSHs( sv_2mortal( newSViv( status ) ) );


-- 
Bill Moseley
[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.