There is always a risk when working with the internals of another module.
I would minimize that risk by making sure that the DESTROY that you are
replacing always runs. Just in case something gets added to it. And
capture the reference to it in a way that will notice if DESTROY is
eliminated.
Like this:
use IO::All;
{
no warnings 'redefine';
my $orig_destroy = *IO::ALL::DESTROY{CODE};
sub IO::All::DESTROY {
my $self = shift;
$orig_destroy->($self, @_) if $orig_destroy;
no warnings;
untie *$self if tied *$self;
$self->close if $self->is_open;
}
}
On Tue, Dec 13, 2016 at 10:04 AM, Duane Bronson <[email protected]>
wrote:
> Mongers,
>
> I've been trying to track down a memory leak in a long running perl script
> that uses IO::All to write a bunch of files every few seconds. I traced my
> leak back to IO::All which checks to make sure the version is at least
> 5.8.0 before calling untie. Pretty innocuous, imho.
>
> Here's an easy way to reproduce it. In perl 5.10.1, this script causes
> perl's memory to grow really big. It appears to be fixed in later versions
> of perl because there is no memory leak on my mac (perl 5.18.2).
>
> perl -E 'for $i (0..100_000_000) { 1 if ($^V) }'
> and
> perl -E '$foo = version->new(v1.2.3); for $i (0..100_000_000) { 1 if
> ($foo) }'
> and (slower)
> perl -E 'use IO::All; for $i (0..100_000_000) { "file contents" >
> io("/tmp/filename") }'
>
> Is there a way of fixing my script so I can still use IO::All and not have
> a memory leak? Here is one way, but I suspect there are better methods
> that won't break with a future version of IO::All.
>
> use IO::All;
> {
> no warnings 'redefine';
> sub IO::All::DESTROY {
> my $self = shift;
> no warnings;
> #unless ( $^V and $^V lt v5.8.0 ) {
> untie *$self if tied *$self;
> #}
> $self->close if $self->is_open;
> }
> }
>
> Thanks,
> Duane
>
>
>
> Duane Bronson
> [email protected] <mailto:[email protected]>
> http://www.nerdlogic.com/ <http://www.nerdlogic.com/>
> 5 Goden St.
> Belmont, MA 02478
> 617.515.2909
>
>
>
>
>
> _______________________________________________
> Boston-pm mailing list
> [email protected]
> http://mail.pm.org/mailman/listinfo/boston-pm
>
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm