Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Today while testing code I ran across an error that was new to me: "Attempt to free unreferenced scalar: SV 0x8238030, Perl interpreter: 0x817b008 during global destruction.". I've been able to isolate the issue to the code at the end of this post.

It seems to be due to some interaction between tied handles, select, temporary file handles created by File::Temp, and number of lines of code in the module(!). Merely by commenting out one line of code in a method that is never even called, I can make Perl stop complaining for 2 of the 3 complaining cases.

I'm guessing this is a Perl bug because commenting out a line that is never executed shouldn't change the behavior of a script, but I'd like another set of eyes. Is this just on my build/platform: Debian (Lenny) - system perl (5.10.0)? Am I using something in a way that was never intended and so confusing perl?

As can be seen from the script below, I've tried a number of different types of file handles (string backed, plain, STDOUT, STDERR, permanent temp files) and the only kind of handle that consistently shows a problem is a runtime only temp file: i.e. a nameless temp file handle or a named file handle that is supposed to go away when the program dies.

Also I'm curious what is the Perl interpreter doing that it is so sensitive to a line commented out? Memory corruption? Something else?

The test script I'm using is posted below.

use strict; use warnings; use File::Temp; use Symbol; { package MyWrapper; # VERY STRANGE - although this method is never called, if one # comments one or more lines below complains stop sub xxx { my $x=1; #comment this out:'temp', 'temp_keep' stop complaining my $y=2; my $z=3; } sub TIEHANDLE { my ($sClass, $self, $fh) = @_; $self = bless($self, $sClass); ${*$self}{io_window_fh} = $fh; return $self; } sub PRINT { my $self = shift; my $fh = ${*$self}{io_window_fh}; CORE::print $fh "@_"; } sub DESTROY { print STDERR "$_[0]: I'm dying...dead\n"; } } #--------------------------------------------------------------- # Select File Handle for test #--------------------------------------------------------------- # These complain: attempt to free unreferenced scalar # during global destruction my $fh; if (!$ARGV[0] || ($ARGV[0] eq 'temp')) { $fh = File::Temp::tempfile( UNLINK => 1); } elsif ($ARGV[0] eq 'temp_keep') { # This also complains $fh = File::Temp::tempfile( UNLINK => 0); } elsif ($ARGV[0] eq 'temp_unlink_name') { ($fh, my $name) = File::Temp::tempfile( UNLINK => 1); # BUT ALL THESE ARE OK } elsif ($ARGV[0] eq 'temp_keep_name') { ($fh, my $name) = File::Temp::tempfile( UNLINK => 0); } elsif ($ARGV[0] eq 'buf') { # no complaints with this file handle my $sBuf=''; open $fh, '+>', \$sBuf; } elsif ($ARGV[0] eq 'stdout') { # nor with this one $fh = \*STDOUT; } elsif ($ARGV[0] eq 'stderr') { # nor with this one $fh = \*STDERR; } else { # nor with this one open ($fh, '>', $ARGV[0]); } #--------------------------------------------------------------- # Demonstrate bug #--------------------------------------------------------------- my $fhWrapper = Symbol::gensym; tie(*$fhWrapper, 'MyWrapper', $fhWrapper, $fh); select $fhWrapper; print "Hello World\n"; print STDOUT "Done\n"; END { print STDERR "Global destruction beginning\n"; }

In reply to Temp file handles refcount bug? by ELISHEVA

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found