in reply to Re: Re: Correct way to return a reference in XS/embedded perl
in thread Correct way to return a reference in XS/embedded perl

Yes, mine returns a status value via the RETVAL, but of course this is just an interger.

So then since a class is just a fancy hash I guess the obvious candidate for the villain in your tale is the garbage collector. If the reference counts are not set then it could free up the elements before you have finished with them. Are you sure that you have set the reference counts on both the hash and the reference?

In buildA() I see one call to sv_2mortal() but don't see how the code

tmpSv = POPs; a_sv = newSVsv(tmpSv); // or use NEWSV/SvSetMagicSV

is ensuring the ref counts are set (and my Perl documents are 8 time zones away, so this is only a guess).

Generally I call sv_2mortal() on anything I send back to Perl (and on things that they reffer to and so on). Here is a place where I return a list of strings to Perl (I suspect this is almost exactly strait from the Perl docs)

void SglCreateProjectNameList() PPCODE: { char **names; int count,i; SglStatus ret; ret = SglCreateProjectNameList(NULL,&names,&count); if(ret != SGL_SUCCESS) XSRETURN_EMPTY; /* Here we take a belt and braces approach, the fact that we precalculate the length of the list (and call EXTEND +) means that we could use PUSHs rather than XPUSHs, but it doesn't cost much to be paranoid */ EXTEND(SP,count+1); for(i = 0; i < count; i++) XPUSHs(sv_2mortal(newSVpvn(names[i], 1+strlen(names[i] +)))); SglFreeNameList(NULL,names); }

Of course you have to be carefull with ref counts otherwise your script will run out of memory.

Hope that was more valuable than my last post :-)