Steven N. Hirsch wrote:

On Mon, 10 Mar 2003, Steve Hay wrote:



Hi,

I have a memory leak problem in some XS code that populates a (passed in) hash with (newly created) hash references.

Here is a sample XS routine and Perl wrapper which reproduces the problem:

# XS
void
_fooxs(href)
   INPUT:
       HV *href;

   PREINIT:
       int i;
       int keylen;
       char key[6];
       HV *hash;

PROTOTYPE: $

   CODE:
       for (i = 0; i < 100; i++) {
           hash = newHV();
           hv_store(hash, "string", 6, newSVpv("one", 3), 0);
           hv_store(hash, "number", 6, newSViv(1), 0);
           keylen = sprintf(key, "key%02d", i);
           hv_store(href, (const char *) key, keylen,
               newRV_noinc((SV *) hash), 0);
       }

OUTPUT:
href


^^^^^^^^
Not sure if this is the problem, but why are you declaring an output value when the function does not return anything? Taking a close look at the generated C code can be of help to see what might be going wrong.


Yes, you're quite right: If I simply omit the last two lines of the XSUB then it all works fine.

The reason that I'd declared an output value in the first place was that EXAMPLE 3 in the "perlxstut" manpage did this in an XSUB which modified its argument. But, of course, that's not quite what's happening here because the XSUB doesn't modify it's argument -- it modifies what is pointed to by it's argument.

Thanks for spotting the mistake!

Steve



Reply via email to