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.


In reply to Re: unreachable memory with a non-zero reference count by JavaFan
in thread unreachable memory with a non-zero reference count by Pickwick

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.