Hello all,

This is my first attempt at using Inline.  I'm trying to write a function that 
duplicates this perl subroutine:

sub _init_values_perl {
   my ( $this, $col_vals ) = @_;

   while ( my ($k, $v) = each %$col_vals ) {
      $this->{'_col_' . $k} = $v;
   }
}

This is what I have got so far:

void _init_values( SV* obj, SV* col_vals ) {
   HV*    hash;
   HE*    hash_entry;
   SV*    sv_key;
   SV*    sv_val;
   HV*    members    = SvRV(obj);
   SV*    sv_new_key = newSVpv( "_col_", 5 );
   char*  key;
   int    num_keys, i;
   STRLEN keylen;

   hash = (HV*)SvRV( col_vals );
   num_keys = hv_iterinit( hash );
   for ( i = 0; i < num_keys; i++ ) {
      hash_entry = hv_iternext( hash );
      sv_key     = hv_iterkeysv( hash_entry );
      sv_val     = hv_iterval( hash, hash_entry );

      sv_setpvn( sv_new_key, "_col_", 5 );
      sv_catsv( sv_new_key, sv_key );
      key = SvPV( sv_new_key, keylen );

      hv_store( members, key, keylen, sv_val, 0 );
   }
   hv_iterinit( hash );
   return;
}

When I run this, I get 'Attempt to free unreferenced scalar' warnings.  If I 
try to run it multiple times I will eventually get segmentation fault.  So 
after reading perlguts and scanning this list I changed the hv_store call to 
be:

      hv_store( members, key, keylen, SvREFCNT_inc(sv_val), 0 );

Now everything works, but I'm getting memory leaks.  I've also tried to clone 
sv_val using newSVsv() but with the same results. I'm not sure what I'm doing 
wrong.

This is how I'm calling the methods:

my $t = {
   foo => 'bar',
   tom => 'bombadil',
   joe => 'smith',
   jon => 'parker',
};

my $inline_d = DumpValsInline->new();
my $perl_d   = DumpValsInline->new();

cmpthese( $ARGV[0], {
   perl   => sub { $perl_d->_init_values_perl( $t ) },
   inline => sub { $inline_d->_init_values( $t ) }
});

Thanks in advance,
Shane Corgatelli
[EMAIL PROTECTED]

    

Reply via email to