Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in tied Hash #8979

Open
p5pRT opened this issue Jul 30, 2007 · 5 comments
Open

Memory leak in tied Hash #8979

p5pRT opened this issue Jul 30, 2007 · 5 comments

Comments

@p5pRT
Copy link

p5pRT commented Jul 30, 2007

Migrated from rt.perl.org#44253 (status was 'open')

Searchable as RT44253$

@p5pRT
Copy link
Author

p5pRT commented Jul 30, 2007

From holger.seelig@yahoo.de

Hello,

Creating copies of blessed objects in a tied hash during overloaded
operators leaks memory.
This bug is similar to ticket #44217.

The code below describes the problem.

package Tie​::Objects;
use strict;
use warnings;

use Tie​::Hash;
use base 'Tie​::StdHash';

sub TIEHASH { bless \$_[1], $_[0] }

sub FETCH { ${ $_[0] }->{ $_[1] }->getClone }

sub STORE { ${ $_[0] }->{ $_[1] }->setValue( $_[2] ) }

package Vec2;
use strict;
use warnings;

use overload '""' => sub { join " ", @​{ $_[0] } };
use overload '+' => sub { $_[0]->new( [ map { $_ + $_[1] } @​{ $_[0] } ] ) };

sub new { bless $_[1] || [ 0, 0 ], 'Vec2' }

sub DESTROY { print "DESTROY Vec2"; 0; }

package Object;
use strict;
use warnings;

use overload '""' => sub { $_[0]->{value} };
use overload '+' => sub { $_[0]->{value} + $_[1] };

sub new { bless { value => new Vec2 $_[1] }, 'Object' }

sub getClone { $_[0]->new( [ @​{ $_[0]->{value} } ] ) }

sub getValue { $_[0]->{value} }
sub setValue { $_[0]->{value} = $_[1] }

package main;
use strict;
use warnings;
$\ = "\n";

my $objects = {};

$objects->{v1} = new Object( [ 1, 1 ] );
tie my %tiedObjects, 'Tie​::Objects', $objects;

print "# Block 1 ################";
{
  print $tiedObjects{v1}++;
  print $tiedObjects{v1}++;
  print $tiedObjects{v1}++;
}

print "# END ###############";

1;
__END__

Output of the code above​:

# Block 1 ################
DESTROY Vec2
1 1
DESTROY Vec2
2 2
DESTROY Vec2
3 3
# END ###############
DESTROY Vec2
DESTROY Vec2
DESTROY Vec2
DESTROY Vec2

As you can see the the tree copies of Vec2 created in the tied hash are
only now destroyed at
the end of the program, that leaks memory.

Yours sincerely
Holger Seelig

holger@​laptop$ uname -a
Linux laptop 2.6.20-16-generic #2 SMP Thu Jun 7 20​:19​:32 UTC 2007 i686
GNU/Linux

holger@​laptop$ perl -v

This is perl, v5.8.8 built for i486-linux-gnu-thread-multi

Copyright 1987-2006, Larry Wall

Perl may be copied only under the terms of either the Artistic License
or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http​://www.perl.org/, the Perl Home Page.

holger@​laptop$ perl -V
Summary of my perl5 (revision 5 version 8 subversion 8) configuration​:
Platform​:
  osname=linux, osvers=2.6.15.7, archname=i486-linux-gnu-thread-multi
  uname='linux rothera 2.6.15.7 #1 smp sat sep 30 10​:21​:42 utc 2006
i686 gnulinux '
  config_args='-Dusethreads -Duselargefiles -Dccflags=-DDEBIAN
-Dcccdlflags=-fPIC -Darchname=i486-linux-gnu -Dprefix=/usr
-Dprivlib=/usr/share/perl/5.8 -Darchlib=/usr/lib/perl/5.8
-Dvendorprefix=/usr -Dvendorlib=/usr/share/perl5
-Dvendorarch=/usr/lib/perl5 -Dsiteprefix=/usr/local
-Dsitelib=/usr/local/share/perl/5.8.8
-Dsitearch=/usr/local/lib/perl/5.8.8 -Dman1dir=/usr/share/man/man1
-Dman3dir=/usr/share/man/man3 -Dsiteman1dir=/usr/local/man/man1
-Dsiteman3dir=/usr/local/man/man3 -Dman1ext=1 -Dman3ext=3perl
-Dpager=/usr/bin/sensible-pager -Uafs -Ud_csh -Uusesfio -Uusenm
-Duseshrplib -Dlibperl=libperl.so.5.8.8 -Dd_dosuid -des'
  hint=recommended, useposix=true, d_sigaction=define
  usethreads=define use5005threads=undef useithreads=define
usemultiplicity=define
  useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
  use64bitint=undef use64bitall=undef uselongdouble=undef
  usemymalloc=n, bincompat5005=undef
Compiler​:
  cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS
-DDEBIAN -fno-strict-aliasing -pipe -I/usr/local/include
-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
  optimize='-O2',
  cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBIAN
-fno-strict-aliasing -pipe -I/usr/local/include'
  ccversion='', gccversion='4.1.2 (Ubuntu 4.1.2-0ubuntu4)', gccosandvers=''
  intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
  d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
  ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t',
lseeksize=8
  alignbytes=4, prototype=define
Linker and Libraries​:
  ld='cc', ldflags =' -L/usr/local/lib'
  libpth=/usr/local/lib /lib /usr/lib
  libs=-lgdbm -lgdbm_compat -ldb -ldl -lm -lpthread -lc -lcrypt
  perllibs=-ldl -lm -lpthread -lc -lcrypt
  libc=/lib/libc-2.5.so, so=so, useshrplib=true, libperl=libperl.so.5.8.8
  gnulibc_version='2.5'
Dynamic Linking​:
  dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
  cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib'

Characteristics of this binary (from libperl)​:
Compile-time options​: MULTIPLICITY PERL_IMPLICIT_CONTEXT
  PERL_MALLOC_WRAP THREADS_HAVE_PIDS USE_ITHREADS
  USE_LARGE_FILES USE_PERLIO USE_REENTRANT_API
Built under linux
Compiled at Mar 6 2007 01​:40​:14
@​INC​:
  /etc/perl
  /usr/local/lib/perl/5.8.8
  /usr/local/share/perl/5.8.8
  /usr/lib/perl5
  /usr/share/perl5
  /usr/lib/perl/5.8
  /usr/share/perl/5.8
  /usr/local/lib/site_perl
  /usr/local/lib/perl/5.8.7
  /usr/local/share/perl/5.8.7
  .
holger@​laptop$

@p5pRT
Copy link
Author

p5pRT commented Jul 30, 2007

From holger.seelig@yahoo.de

TieHash1.pl

@p5pRT
Copy link
Author

p5pRT commented Jul 31, 2007

From l2ot9pa02@sneakemail.com

Holger Seelig schrieb​:

# New Ticket Created by Holger Seelig
# Please include the string​: [perl #44253]
# in the subject line of all future correspondence about this issue.
# <URL​: http​://rt.perl.org/rt3/Ticket/Display.html?id=44253 >
[...]
Creating copies of blessed objects in a tied hash during overloaded
operators leaks memory.
This bug is similar to ticket #44217.
[...]
As you can see the the tree copies of Vec2 created in the tied hash are
only now destroyed at
the end of the program, that leaks memory.

This isn't a confirmation that it's actually a problem and not intended
behaviour, but at least, the output when run with bleadperl is
consistent with what Holger reports​:

# Block 1 ################
DESTROY Vec2
1 1
DESTROY Vec2
2 2
DESTROY Vec2
3 3
# END ###############
DESTROY Vec2
DESTROY Vec2
DESTROY Vec2
DESTROY Vec2

Steffen

@p5pRT
Copy link
Author

p5pRT commented Jul 31, 2007

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Jun 23, 2008

From holger.seelig@yahoo.de

I tested the script under ActivePerl-5.10. The scripts output is still
the same as of Steffen run​:

# Block 1 ################
DESTROY Vec2
1 1
DESTROY Vec2
2 2
DESTROY Vec2
3 3
# END ###############
DESTROY Vec2 # has to destoy before end
DESTROY Vec2 # has to destoy before end
DESTROY Vec2 # has to destoy before end
DESTROY Vec2

perl version from terminal​:
holger​:~$ /opt/ActivePerl-5.10/bin/perl -v

This is perl, v5.10.0 built for x86_64-linux-thread-multi
(with 4 registered patches, see perl -V for more detail)

Copyright 1987-2007, Larry Wall

Binary build 1003 [285500] provided by ActiveState
http​://www.ActiveState.com
Built May 13 2008 21​:06​:04

Perl may be copied only under the terms of either the Artistic License
or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http​://www.perl.org/, the Perl Home Page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants