________________________________
> From: mose...@hank.org
> Date: Fri, 2 Nov 2012 22:40:31 -0700
> Subject: Free memory after newSVpv
> To: perl-xs@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 ) ) );
>
Do not confuse Perl's memory allocator as described in perlclib with your C
compiler's or OS's clib's malloc. They are 2 different allocators and the mem
blocks come from 2 different pools (not 100% true, but let us keep this simple
and sane). Read the docs for whatever the sendTask C++ method is and figure out
what the C++ lib's docs tell you to use to free var buffer.