in reply to unreachable memory with a non-zero reference count

Is the problem that $a is declared in the block, but never assigned with something and never used elsewhere?
Considering half of two statements in the code fragment you quote assing to $a what makes you think it's never assigned with something?

Perl uses refcounting to decide when to release memory. This check is done each time a scope is left. The idea is that if there's still a reference to a variable at scope exit, there's likely to be something outside the scope that still refers to the variable - and hence it must be kept. The code you quote is an example of where it may go wrong. Here, $a is assigned a reference. The reference it to itself. At that moment, the variable $a will have a refcount of 2 (its name, and the reference). When the scope is exited, the refcount drops to 1; its name is gone, but the reference still exists. So, $a is not marked as "memory to reuse". Yet $a cannot be referenced by any other code. This is considered a memory leak.

Your code, OTOH, while not very useful, may, or may not, have a memory leak. It has the same issue as the quoted code, $retVal is a reference to itself. Why you first assign $someObject->bar()->doSomething(); to it, then discard the result is not clear to me. Nor is it clear to me what Log4perl->debug should do with its second argument. However, since you return $retVal, it's not a memory leak yet. $retVal's memory will not be marked "free for reuse" (because its refcount hasn't dropped to 0), but since you are returning the value of $retVal (which is a reference to $retVal), you can still reach it. If you discard the return value of foo, it's a memory leak. If you use it to break the circular reference, it's not.

  • Comment on Re: unreachable memory with a non-zero reference count

Replies are listed 'Best First'.
Re^2: unreachable memory with a non-zero reference count
by Pickwick (Beadle) on Jun 21, 2010 at 13:33 UTC
    Why you first assign $someObject->bar()->doSomething(); to it, then discard the result is not clear to me. Nor is it clear to me what Log4perl->debug should do with its second argument.
    It just was an example, because I have methodes which work like that. doSomething() provides a string, but I need a reference to that string to send it to the calle of foo. I thought my line is more readable than $retVal = \$someObject->bar()->doSomething(); or user \$retVal wherever I want the reference. The second parameter to debug is only called when Log4perl actually logs something and used in the log message. The documentation of Log4perl prefers using anonymous subs with references to data when logging large strings because the strings won't be copied if the current log level is not debug.
      Your line may be more readable, it's not returning a reference to the string. It's returning a reference to itself.
      use 5.10.0; use strict; use warnings; sub foo {"A string"}; my $object = bless []; my $retVal1 = \$object->foo; my $retVal2 = $object->foo; $retVal2 = \$retVal2; say "$retVal1: $$retVal1"; say "$retVal2: $$retVal2"; say $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$retVal2; __END__ SCALAR(0x893c2c8): A string REF(0x894d1f4): REF(0x894d1f4) REF(0x8d9b1f4)