Re: Odd (undocumented?) behavior of RAM file within a loop



Ben Morrow <ben@xxxxxxxxxxxx> wrote in
news:8v2575-srt.ln1@xxxxxxxxxxxxxxxxxxxxxxx:


Quoth "A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx>:

I found it curious that the warning disappears if I commented out the
close statement (on AS Perl 5.10 on Win32).

....

Use of uninitialized value $buffer in open at C:\Temp\t.pl line 8.
SCALAR(0x182b04c)

This seems to be an bug in PerlIO::scalar.

<explanation snipped for brevity>

Thank you for your explanation.

....

I've done up a patch, which I'll send to p5p when I've tested it.

Great.


With the close statement commented out, I get:

C:\Temp> t
Pass #1
SCALAR(0x182b04c)
Pass #2
SCALAR(0x22ae14)

This makes less sense... I suspect what's happening here is that the
filehandle isn't being closed until *after* $buffer gets allocated for
the next iteration, so it is forced to allocate a new scalar since the
old one's still in use.

That is my gut feeling, too.

This is arguably wrong, since the whole point is to reuse the old
scalar if we can, for efficiency.

In fact, I would argue that it is wrong. I don't foresee myself being
able to first find where this is occurring and fix it without messing up
anything else.

On the other hand, when I used an infinite loop to see if this behavior
caused a leak, it seemed to just cycle through the same three locations
and memory usage did not increase at all.

C:\Temp> cat leak.pl
#!/usr/bin/perl

use strict;
use warnings;

while ( 1 ) {
open my $fh, '>', \(my $buffer) or die $!;
print \$buffer, "\n";
print $fh "test\n";
sleep 1;
}


C:\Temp> leak
SCALAR(0x182aeb4)
SCALAR(0x22acb4)
SCALAR(0x22ad04)
SCALAR(0x182aeb4)
SCALAR(0x22acb4)
SCALAR(0x22ad04)
SCALAR(0x182aeb4)
SCALAR(0x22acb4)
SCALAR(0x22ad04)
SCALAR(0x182aeb4)
SCALAR(0x22acb4)
SCALAR(0x22ad04)
SCALAR(0x182aeb4)
Terminating on signal SIGINT(2)

C:\Temp> perl -v

This is perl, v5.10.0 built for MSWin32-x86-multi-thread
(with 3 registered patches, see perl -V for more detail)

Copyright 1987-2007, Larry Wall

Binary build 1002 [283697] provided by ActiveState
http://www.ActiveState.com
Built Jan 10 2008 11:00:53

And:

perl leak.pl
SCALAR(0x1002eabc)
SCALAR(0x1002ea98)
SCALAR(0x1002eabc)
SCALAR(0x1002ea98)
SCALAR(0x1002eabc)
SCALAR(0x1002ea98)
SCALAR(0x1002eabc)

perl -v

This is perl, v5.8.8 built for cygwin-thread-multi-64int
(with 8 registered patches, see perl -V for more detail)

Copyright 1987-2006, Larry Wall



--
A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>

.



Relevant Pages

  • dmake.exe: Error: -- C:PerllibConfig.pm not found, and cant be made
    ... This is perl, v5.10.0 built for MSWin32-x86-multi-thread ... (with 5 registered patches, see perl -V for more detail) ... Copyright 1987-2007, Larry Wall ...
    (comp.lang.perl.misc)
  • Re: Problem reading "png", jpg" images with Tk::Photo
    ... This is perl, v5.8.8 built for MSWin32-x86-multi-thread ... (with 18 registered patches, see perl -V for more detail) ... Copyright 1987-2007, Larry Wall ...
    (comp.lang.perl.tk)
  • RE: Installing DBD::File via CPAN
    ... I built this directly from source. ... > Did you build Perl from source, or install via a RPM or binary distro? ... > CPAN module so I wasn't expecting to get myself confused with multiple ...
    (perl.dbi.users)
  • Microsoft ODBC for Oracle problem
    ... This is perl, v5.8.2 built for MSWin32-x86-multi-thread ... ActiveState is a division of Sophos. ... advise the sender and delete the message and attachments immediately. ...
    (perl.dbi.users)
  • (Fwd) DBD module install problem
    ... I downloaded and built from source the lastest Perl from CPAN. ... I built it with gcc compiler which was built as a 64 bit binary. ... I was able to add the DBI module for Oracle, with no problem, but when I during the make of DBD I get this warning and an error message, and am unable to install it. ...
    (perl.dbi.users)