Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Free memory after newSVpv

16 views
Skip to first unread message

Bill Moseley

unread,
Nov 3, 2012, 1:40:31 AM11/3/12
to per...@perl.org
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
mos...@hank.org
0 new messages