in reply to unreachable memory with a non-zero reference count
At the end the ref count of $a is not 0, so the memory is not freed. And since the referencer $a is not available anymore, the count can never be decreased to zero. We have a chunk of memory that has leaked.{ my $a; $a = \$a; # reference count of $a increases to 1 } # $a goes out of scope, but $a stays in use because of it +s reference count
The problem is avoided, when we break the self reference manually at the end.
my $a; $a = \$a; # reference count of $a increases to 1 undef $a; # reference count decreases to 0 } # $a goes out of scope, $a gets freed because of its refe +rence count of 0
So regarding your question Is the problem that $a is declared in the block, but never assigned with something and never used elsewhere?
In your code
you should use two variables, one for the value and one for the reference of the value.$retVal = $someObject->bar()->doSomething(); $retVal = \$retVal;
You don't have the problem of self-referencing then. In its current form it cannot work, because the variable can hold only one of the two things.my $myVal = $someObject->bar()->doSomething(); $retVal = \$myVal;
The answer to your question Does this produces memory leaks like stated in the perl documentation? is yes, since there is nothing to break the circular self reference and thus leaves the ref count to 1.
|
---|