Pickwick has asked for the wisdom of the Perl Monks concerning the following question:
Hello all,
in the perl documentation I've read about some statements that could result in memory leaks, but I don't understand what the real problem is and if some code I use should be avoided because it has the same problem. I would be happy if someone could give me some advise and lighten my darkness.
Let me quote:
A more serious concern is that unreachable memory with a non-zero reference count will not normally get freed. Therefore, this is a bad idea:{ my $a; $a = \$a; }
http://perldoc.perl.org/perlobj.html#Two-Phased-Garbage-Collection
Is the problem that $a is declared in the block, but never assigned with something and never used elsewhere? If I would assign $a a value from somewhere and do use it somewhere else, do I have the same problem?
I have code like the following:
sub foo { my $retVal = undef; $retVal = $someObject->bar()->doSomething(); $retVal = \$retVal; Log4perl->debug('retval: ', sub {$retVal}); return $retVal; }
The goal of the above is to let foo return a reference to a string, but just have to create that reference once and to use it as a return value and for logging purposes. Does this produces memory leaks like stated in the perl documentation?
Thanks for your hints.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: unreachable memory with a non-zero reference count
by Corion (Patriarch) on Jun 20, 2010 at 15:59 UTC | |
by Pickwick (Beadle) on Jun 21, 2010 at 13:21 UTC | |
by Corion (Patriarch) on Jun 21, 2010 at 13:26 UTC | |
by Pickwick (Beadle) on Jun 21, 2010 at 13:45 UTC | |
Re: unreachable memory with a non-zero reference count
by hexcoder (Curate) on Jun 20, 2010 at 20:51 UTC | |
Re: unreachable memory with a non-zero reference count
by JavaFan (Canon) on Jun 20, 2010 at 16:27 UTC | |
by Pickwick (Beadle) on Jun 21, 2010 at 13:33 UTC | |
by JavaFan (Canon) on Jun 22, 2010 at 03:36 UTC |