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);
                             
            • The char* typemap uses newSVpv to turn the char* into an SV*. newSVpv copies the string into the scalar. Two options: use a CLEANUP section to free the string or use a custom typemap that frees the string.

              CLEANUP section: ...
                  CLEANUP:
                      free (ret);

              Custom typemap: in a typemap file:

              TYPEMAP
              char_own * T_CHAR_OWN
              OUTPUT
              T_CHAR_OWN
                              sv_setpv ((SV*)$arg, $var);
                              g_f
        • If you did a SAVEFREEPV() on the pointer it would be added to the savestack and freed the next time the save stack was cleared.

          But you shouldn't use calloc() or other system level memory allocation routines. You should use New() or one of its variants. This is because Perl may be configured to use its own malloc routines and using the system ones at the same time could lead to issues. IOW, it might work fine for you on your box but might go disasterously wrong when built on a Perl with a different configura