I'm writing some Perl bindings for SANE. Having got things working,
I'm trying to fix a couple of memory leaks.

valgrind is complaining:

==7353== Invalid read of size 1
==7353==    at 0x40239E3: strlen (mc_replace_strmem.c:242)
==7353==    by 0x80D0710: Perl_newSVpv (in /usr/bin/perl)
==7353==    by 0x472152F: XS_SANE_open (SANE.xs:126)
==7353==    by 0x80C22D2: Perl_pp_entersub (in /usr/bin/perl)
==7353==    by 0x80C0CAA: Perl_runops_standard (in /usr/bin/perl)
==7353==    by 0x806727A: perl_run (in /usr/bin/perl)
==7353==    by 0x8063791: main (in /usr/bin/perl)
==7353==  Address 0x49840b8 is 0 bytes after a block of size 8 alloc'd
==7353==    at 0x4021BDE: calloc (vg_replace_malloc.c:397)
==7353==    by 0x473CAF2: sane_dll_open (in /usr/lib/libsane.so.1.0.19)
==7353==    by 0x473D193: sane_open (in /usr/lib/libsane.so.1.0.19)
==7353==    by 0x47213D9: XS_SANE_open (SANE.xs:124)
==7353==    by 0x80C22D2: Perl_pp_entersub (in /usr/bin/perl)
==7353==    by 0x80C0CAA: Perl_runops_standard (in /usr/bin/perl)
==7353==    by 0x806727A: perl_run (in /usr/bin/perl)
==7353==    by 0x8063791: main (in /usr/bin/perl)

void
sane_open(class, name)
                SANE_String_Const       name
        INIT:
                SANE_Status             status;
                SANE_Handle             h;
        PPCODE:
                status = sane_open(name, &h);
                XPUSHs(sv_2mortal(newSViv(status)));
126->                XPUSHs(sv_2mortal(newSVpv(h, 0)));

SANE_Handle is a void pointer:

http://www.sane-project.org/html/doc011.html#s4.2.6: Access to a
scanner is provided through an opaque type called SANE_Handle. The C
declaration of this type is given below.

    typedef void *SANE_Handle;

While this type is declared to be a void pointer, an application must
not attempt to interpret the value of a SANE_Handle. In particular,
SANE does not require that a value of this type is a legal pointer
value.

So in the typemap I have:

SANE_Handle                     T_OPAQUEPTR

I assume that newSVpv(h, 0) is causing me the trouble. I can't work
out what is correct.

Any help would be appreciated

Jeff

Reply via email to