Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: SV creation inside XS

by patcat88 (Deacon)
on Nov 07, 2011 at 01:45 UTC ( [id://936380]=note: print w/replies, xml ) Need Help??


in reply to SV creation inside XS

HV* hv = newHV(); RETVAL = newSVrv((SV*)hv, NULL);
As said before, newSVrv is wrong. Make sure you use newRV_noinc since newHV has a ref count of 1 already. If you bring HV to 2 and return the RV to Perl land, the HV is now memory leaked because when the RV is garbage collected, the HV will stay forever. HV and AVs must be returned in RVs to Perl land. If you try to return a HV or AV without it being in an RV, you will get the "bizarre copy" die the next time a perl op sees it. Remember, RETVALed or OUTPUTed SV* get mortalized behind the scenes (look at your .c file), so they should be left by you with refcount 1, and NOT mortalized. As I recall, perl gets very noisy (can't remember if it is a warning or fatal) if your double mortaled SV * or derivative appears. perl will try to delete the SV twice. perl will catch the double mortaled SV because perl will be freeing something already freed and returned by perl to the SV arena/free SV struct cache. Freed SVs have special patterns in them, like refcount in the 2 billions.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-18 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found