Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by nikolaus (Novice)
on Mar 23, 2004 at 17:03 UTC ( [id://339077]=note: print w/replies, xml ) Need Help??


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

One place where ours seem to differ is where mine is trying to return a scalar reference to an instance of a class back to Perl; I don't see where yours is returning anything to Perl. Am I missing something?
  • Comment on Re: Re: Correct way to return a reference in XS/embedded perl

Replies are listed 'Best First'.
Re^3: Correct way to return a reference in XS/embedded perl
by hawtin (Prior) on Mar 24, 2004 at 03:51 UTC

    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 :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://339077]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2024-04-24 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found