Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: odd line in windows

by ikegami (Pope)
on Sep 08, 2011 at 09:26 UTC ( #924748=note: print w/replies, xml ) Need Help??


in reply to Re^2: odd line in windows
in thread odd line in windows

The typemap automatically creates a reference to it and returns the reference. It's refcount is off by one, though.

use strict; use warnings; use Devel::Peek qw( Dump ); use Inline C => <<'__EOI__'; AV* f() { return newAV(); } __EOI__ Dump(f());
>perl -MInline=FORCE,NOISY,NOCLEAN a.pl ... SV = IV(0x7bbf18) at 0x7bbf1c REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x7bc05c SV = PVAV(0x7bcec8) at 0x7bc05c REFCNT = 2 <--- XXX FLAGS = () ARRAY = 0x0 FILL = -1 MAX = -1 ARYLEN = 0x0 FLAGS = (REAL)

Making it mortal doesn't help. So unless I'm missing something, this can only be used if you actually hold a reference to the array you return.

Replies are listed 'Best First'.
Re^4: odd line in windows
by syphilis (Chancellor) on Sep 08, 2011 at 10:58 UTC
    So unless I'm missing something, this can only be used if you actually hold a reference to the array you return

    No way to alter that particular reference count before returning the newly created AV ?

    Cheers,
    Rob

      According to the example in the Inline C Cookbook, the correct way to return an AV* is:

      newRV_noinc( (SV*)array = newAV() )

      Which kind of makes sense given the info ikegami posted above.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Which kind of makes sense given the info ikegami posted above

        Yes, it does make sense - yet it's not what I naively expected.
        I expected that you'd just return the AV* ... but if that can bugger up the refcount, and there's no way of twiddling it, then one certainly needs to be careful whenever one declares a function as returning AV*.

        Still, it was probably a silly question on my part.
        If there was a way to twiddle that refcount in a returning AV*, then I would think ikegami would have mentioned it.

        Cheers,
        Rob
        MUTABLE_SV(av) is better (SV*)av because it won't cast away const.

      I filed a ticket and was told that this is documented in perlxs.


      This is quite useful as it usually improves readability. While this works fine for an SV *, it's unfortunately not as easy to have AV * or HV * as a return value. You should be able to write:

      AV * array() CODE: RETVAL = newAV(); /* do something with RETVAL */ OUTPUT: RETVAL

      But due to an unfixable bug (fixing it would break lots of existing CPAN modules) in the typemap file, the reference count of the AV * is not properly decremented. Thus, the above XSUB would leak memory whenever it is being called. The same problem exists for HV *.

      No. Decrementing its refcount before returning it would free it. One would normally make it a mortal, but I tried that and it didn't work.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://924748]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2017-10-21 02:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (269 votes). Check out past polls.

    Notices?