Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • I'm guessing based on the C parts since I don't know XS very well.

    Aren't you calloc()ing a char buffer without free()ing it.

    John.
    --
    • Yes, but perl should free that when the SvREFCOUNT reaches zero.
      • Never mind, that does indeed appear to be the culprit. How strange - I could have sworn perl free'd the strings.
        • The string (ret) is copied from the svpv, and not maintained by any sv so it's not perl's responsibility to manage the memory allocated to it.
          • What's weird is this pretty much guarantees that a char * typemap will leak (as you can't free it before you return) unless you are returning static char *s.
            • Why are you returning a char * anyway? Why dont you return an SV? Something like this untested version of your code. Also doing *foo++; is pretty misleading since its the same as foo++;.

              SV *
              getline(str)
                      SV * str
                      CODE:
                      {
                              STRLEN len;
                              char *cval = SvPV(str, len);
                              SV* retsv;
                              I32 taken = 0;
                              char *ret_str;
                              if (!SvOK(str)) {
                                      XSRETURN_UNDEF;
                              }
                              retsv= newSVpvn("",0);
                              SvGROW(retsv,len+1);
                              ret_str = SvPV_nolen(retsv);
                              if (SvTYPE(str) SVt_PVIV)
                                      sv_upgrade(str,SVt_PVIV);
                              RETVAL = retsv;
                              while (*cval) {
                                      *ret = *cval;
                                      if (*cval == '\n')
                                              break;
                                      cval++; ret_str++; taken++;
                              }
                              if (*cval == '\n') {
                                      cval++; ret_str++; taken++;
                              }
                              *ret_str = 0;
                              SvCUR_set(retsv,taken);
                              if (!SvOOK(str)) {
                                      SvOOK_on(str);
                                      SvIVX(str) = 0;
                              }
                              SvIVX(str) += taken;
                              SvPVX(str) += taken;
                              SvLEN(str) -= taken;
                              SvCUR(str) -= taken;
                      }
                      OUTPUT:
                          RETVAL