Hello all,

in the perl documentation I've read about some statements that could result in memory leaks, but I don't understand what the real problem is and if some code I use should be avoided because it has the same problem. I would be happy if someone could give me some advise and lighten my darkness.

Let me quote:

A more serious concern is that unreachable memory with a non-zero reference count will not normally get freed. Therefore, this is a bad idea:
{ my $a; $a = \$a; }

http://perldoc.perl.org/perlobj.html#Two-Phased-Garbage-Collection

Is the problem that $a is declared in the block, but never assigned with something and never used elsewhere? If I would assign $a a value from somewhere and do use it somewhere else, do I have the same problem?

I have code like the following:

sub foo { my $retVal = undef; $retVal = $someObject->bar()->doSomething(); $retVal = \$retVal; Log4perl->debug('retval: ', sub {$retVal}); return $retVal; }

The goal of the above is to let foo return a reference to a string, but just have to create that reference once and to use it as a return value and for logging purposes. Does this produces memory leaks like stated in the perl documentation?

Thanks for your hints.


In reply to 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.