On Wed, 29 Jan 2003, Sisyphus wrote: > ----- Original Message ----- > From: "Miah Gregory" <[EMAIL PROTECTED]>
> If you push it as a 'sv_2mortal()', then you don't have to worry about > memory leaks. I've tried to use sv_newmortal(), which it looks like should do the right thing. > For dynamic memory allocation use New(), Newz(), or Newc() - and Safefree() > instead of free(). I've opted for savepv and savepvn for this particular exercise, as they seem to do most of what I want for me. > > > > Where can I find out more about things like newSVpvf and the rest of > > > > these macros/functions? > See 'perldoc perlapi'. > Also worth taking a look at 'perldoc perlclib'. That's great. Those have helped a lot. It now works as intended, except for one problem. It seems to leak memory, and I'm not entirely sure why, or indeed from where, as my version of perl doesn't have memory debugging (debug version from debian). Could someone point out my obvious mistake? I realise that the code is probably overly verbose; I didn't want to tidy it up until it fully works. Apologies in advance if code snippets aren't welcome on the list. Let me know so I don't do it twice. :-) -snip- void csplitatoffset(SV *data, int offset) { SV *a; SV *b; char *dataptr; char *aptr; char *bptr; STRLEN datalength; STRLEN aptrlength; STRLEN bptrlength; Inline_Stack_Vars; Inline_Stack_Reset; /* create two new stack variables */ a = sv_newmortal(); b = sv_newmortal(); /* get a pointer to the actual data, and the length of that data */ dataptr = SvPV(data, datalength); /* check to see if the requested offset is out of range */ if (offset > datalength) { /* copy the original string, and set the second half to be empty*/ aptr = savepvn(dataptr, datalength); bptr = savepv(""); aptrlength = datalength; bptrlength = 0; } else { /* copy and split the string */ aptr = savepvn(dataptr, offset); bptr = savepvn(dataptr + offset, datalength - offset); aptrlength = offset; bptrlength = datalength - offset; } /* add the strings to the appropriate stack variables */ sv_setpvn(a, aptr, aptrlength); sv_setpvn(b, bptr, bptrlength); /* push the results onto the stack */ Inline_Stack_Push(a); Inline_Stack_Push(b); Inline_Stack_Done; } -snip- Sort of unrelated, I notice that there are a lot of functions with names like xxx_mg, whose descriptions talk about handling 'set' magic. What does this mean? Many thanks in advance. PS. I'm subscribed to the list, so you don't need to CC me. :-) -- Miah Gregory