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.
|
---|
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 | |
by JavaFan (Canon) on Jun 22, 2010 at 03:36 UTC |