Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Not able to pass the hash reference to callback from xsubpp

by sriniiyer (Initiate)
on Aug 13, 2013 at 12:06 UTC ( #1049260=perlquestion: print w/ replies, xml ) Need Help??
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?

Comment on Not able to pass the hash reference to callback from xsubpp
Select or Download Code
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
Re: Not able to pass the hash reference to callback from xsubpp
by ikegami (Pope) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1049260]
Approved by ww
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-08-21 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (127 votes), past polls