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.