I have a memory leak in an xsub, and I have a small example that
illustrates my problem. I just can't figure out what I'm doing wrong.
I would have thought that the following two functions (one in C, the
other in Perl) would be exactly equivalent. But if I run the C
version in a big loop it uses increasingly more memory, whereas the
Perl version stays constant. Can someone explain my error? Thanks.
- Rob
sub LEAK::new
{
my($pkg, $s) = @_;
my($x) = 'x' x $s;
return bless( \$x, $pkg );
}
XS(XS_LEAK_new)
{
dXSARGS;
char * pkg = SvPV(ST(0),na);
int n = SvIV(ST(1));
char * buf;
Newz( 2001, buf, n+1, char ); /* all set to '\0' */
memset( buf, 'x', n);
ST(0) = newRV( sv_2mortal(newSVpv( buf, n )) );
sv_bless( (SV*)ST(0), gv_stashpv(pkg, 1) );
sv_2mortal( ST(0) );
XSRETURN(1);
}
illustrates my problem. I just can't figure out what I'm doing wrong.
I would have thought that the following two functions (one in C, the
other in Perl) would be exactly equivalent. But if I run the C
version in a big loop it uses increasingly more memory, whereas the
Perl version stays constant. Can someone explain my error? Thanks.
- Rob
sub LEAK::new
{
my($pkg, $s) = @_;
my($x) = 'x' x $s;
return bless( \$x, $pkg );
}
XS(XS_LEAK_new)
{
dXSARGS;
char * pkg = SvPV(ST(0),na);
int n = SvIV(ST(1));
char * buf;
Newz( 2001, buf, n+1, char ); /* all set to '\0' */
memset( buf, 'x', n);
ST(0) = newRV( sv_2mortal(newSVpv( buf, n )) );
sv_bless( (SV*)ST(0), gv_stashpv(pkg, 1) );
sv_2mortal( ST(0) );
XSRETURN(1);
}