Perl XS: memory management

I am completely new to Perl XS.

My simple test function gets a string and adds something. Perl has a scalar string in and one.

In the function, I have malloc. What is the right way to free up memory?

SV *foo (str) SV *str CODE: unsigned char *strbuf; size_t strlen; strbuf = (unsigned char *) SvPV (str, strlen); int n = strlen + 10; unsigned char *buf = malloc (n); strncpy (buf, strbuf, strlen); strncat (buf, "0123456789", 10); RETVAL = newSVpv (buf, n); OUTPUT: RETVAL 

thanks! Chris

+5
source share
1 answer

newSVpv creates an internal copy of the string, so you can just free up memory by calling free after assigning RETVAL .

+4
source

All Articles