in reply to Re: Perl XS: garbage-collecting my malloc'd buffer (grow)
in thread Perl XS: garbage-collecting my malloc'd buffer

I think that before you SvGROW you should call SvPV_force. Otherwise (at least this was once true) SvGrow will choke if there is not already a buffer. So, the code would be:
CODE: { int dummylen; SvPV_force(buffer, dummylen); char *buf = SvGROW( buffer, len ); RETVAL = my_func( p_struct, buf, len ); }

Replies are listed 'Best First'.
Re: Re: Re: Perl XS: garbage-collecting my malloc'd buffer
by Elian (Parson) on Mar 03, 2003 at 18:06 UTC
    Yep, that's still the case. SvGROW checks xpv_len, which'll be some bizarre value if the SV's just an IV or NV. Undef might well give it fits too, at least so reads the 5.8.0 sources.

      Wouldn't calling sv_grow() directly fix that?

                      - tye
        Yep, it would. SvGROW tries to be clever, and fails, unfortunately. (And the docs discourage using sv_grow directly, just to compound the potential problems)
Re^3: Perl XS: garbage-collecting my malloc'd buffer (SvGROW)
by tye (Sage) on Mar 03, 2003 at 18:12 UTC

    That was how I had originally written the code in my head then I checked the Perl v5.6.0 source code for SvGROW() and saw that such wasn't needed. It might be a problem for Perl versions prior to that, however. I know I jumped through more hoops when I wrote similar XS code ages ago -- I was hoping it had just been my lack of understanding. (:

    Update: I was mostly checking sv_grow() which is what the SvGROW() macro calls. See elsewhere in this thread for why I'm wrong above.

                    - tye