http://www.perlmonks.org?node_id=1049260

sriniiyer has asked for the wisdom of the Perl Monks concerning the following question:

This is in continuation to the question that I asked in Callback function in xsubpp. While writing another function in a xsubpp program, I wrote something like this:

HV *data_hash = newHV(); /* do some processing here. */ call_perl_sub("test_callback", SvREFCNT_inc(data_hash));

Here the function call_perl_sub() and subroutine test_callback are same as the one given in Callback function in xsubpp. However, when the callback is executed, it doesn't pass the reference to the data_hash that I passed to the callback subroutine and gives me the Bizarre copy of HASH error.

I checked the reference counts using SvREFCNT(data_hash) and found to be correct. Is there something that I am missing while passing it to the callback?

Replies are listed 'Best First'.
Re: Not able to pass the hash reference to callback from xsubpp
by ikegami (Patriarch) on Aug 13, 2013 at 13:36 UTC

    Subs can only take scalars as arguments; you can't pass a hash. You can pass a reference to the hash, though.

    Also, you hold two references to the hash for no reason.

    You want

    HV* data_hash = newHV(); SV* data_hash_ref = newRV_inc(data_hash); ... call_perl_sub("test_callback", data_hash_ref); ... SvREFCNT_dec(data_hash_ref); /* Or motalise and return to caller */ SvREFCNT_dec(data_hash);

    Since we know data_hash_ref will outlive data_hash, we can avoid a ref count increment and decrement pair and simplify the above to the following:

    HV* data_hash = newHV(); SV* data_hash_ref = newRV_noinc(data_hash); ... call_perl_sub("test_callback", data_hash_ref); ... SvREFCNT_dec(data_hash_ref); /* Or motalise and return to caller */

      Thank you ikegami!

      That worked perfectly.

Re: Not able to pass the hash reference to callback from xsubpp
by Anonymous Monk on Aug 13, 2013 at 12:23 UTC
    Could you save me some typing and post a patch? To produce do  make realclean first then diff -ruN empty MinimalExample > sriniiyer.patch