In order to understand, lets see what happens

{ 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
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.

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?

  • '$a declared in a block': Once $a goes out of scope it only gets freed, if its ref count is zero.
  • 'never assigned with something': No, here it is assigned with the reference to itself (which seems rather useless). This increases its own reference count to 1.
  • 'never used elsewhere': As long as you don't have circular references, it does not matter, how much it is used elsewhere.
  • In your code

    $retVal = $someObject->bar()->doSomething(); $retVal = \$retVal;
    you should use two variables, one for the value and one for the reference of the value.
    my $myVal = $someObject->bar()->doSomething(); $retVal = \$myVal;
    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.

    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.


    In reply to Re: unreachable memory with a non-zero reference count by hexcoder
    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.