Related to ref to read-only alias ... why??

I don't think so.

This demonstrates the problem (bug):

perl -E"$x=chr(0); $x x= 100e6; <>; $r = \substr $x, 50e6; <>"

Watch the process memory with ProcessManager:

  1. When it pauses the first time memory usage is ~98MB. Hit enter
  2. Now the memory usage shoots up to ~145MB.

    All that's changes is that it has taken a substr ref to the second half of the string.

I suspect it is a result of this "fix".

But I believe that fix is wrong, and that the original bug is spurious. (At least in part.)

If you do this:

my $ref; { my $string = "123"; $ref = \$string; } ## Here $string persists.

No one is surprised that (the memory allocated to) $string persists beyond the block.

Why should this be any different:

my $ref; { my $string = "123"; $ref = \substr $string, 1, 1; } ## This was the (IMO false) "bug" scenario.

In this case -- when the string goes out of scope -- I could make arguments for 3 possible outcomes:

  1. The memory allocated to $string persists:

    $ref continues to refer (directly) to the memory allocate to $string.

    Those parts of $string outside of $ref are inaccessible and irrecoverable until $ref is GC's.

  2. The memory allocated to $string is GC'd; $ref becomes undef.

    Probably difficult to orchestrate.

  3. The memory allocated to $string persists until $ref is used the next time:

    At which point the extraneous (pre and/or post fix) bits of $string are GC'd leaving $ref pointing at just that which it references.

    This would require retaining a pointer back to 'parent' string with the lvalue ref, and when the lvalue magic fires it checks the refcount of its parent.

    If that refcount is 1 -- meaning it holds the only reference to it, it frees off the extraneous parts of the parent.

Of the three, I'd prefer C, but would find A completely in keeping with Perl's philosophy.

Duplicating the referenced memory of every lvalue ref and the having to copy it back each time it is modified -- just to cover off a really obscure scenario that is at worst, only mildly anomalous -- is crazy.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


In reply to Re^6: Tracking down an Lvalue bug? by BrowserUk
in thread Tracking down an Lvalue bug? by BrowserUk

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.