At this point I have:
void hello(...) PREINIT: char *chartext;
CODE: Newz(0, chartext, 13, char); strcpy(chartext, "Hello, world"); ST(0) = newSV(0); sv_usepvn(ST(0), chartext, 13); XSRETURN(1);
void callhello() PREINIT: SV *val;
CODE: dSP; PUSHMARK(SP); PUTBACK; call_pv("Foo::hello", G_SCALAR); SPAGAIN; val = POPs; PUTBACK; ST(0) = val; PUTBACK; XSRETURN(1);
There's one other problem too: The above code leaks if I call callhello() a million times over in a for loop. Where is the leak coming from?
Doh! Ignore that question -- the leak is, of course, from the SV * created in hello() and never free()'d. The CODE block should be:
Newz(0, chartext, 13, char); strcpy(chartext, "Hello, world"); ST(0) = sv_2mortal(newSV(0)); sv_usepvn(ST(0), chartext, 13); XSRETURN(1);
and the other style that used an SV * with its own char * space should have free()'d the char * space that it had copied from:
Newz(0, chartext, 13, char); strcpy(chartext, "Hello, world"); ST(0) = sv_2mortal(newSVpv(chartext, 13)); Safefree(chartext); XSRETURN(1);
- Steve