Skip Menu |

This queue is for tickets about the HTML-Parser CPAN distribution.

Report information
The Basics
Id: 87840
Status: resolved
Priority: 0/
Queue: HTML-Parser

People
Owner: Nobody in particular
Requestors: KNI [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: memory leak when daughter handler is used
Hello. I have noticed memory leak when daughter handler is used. The code for reproducing is below. $| = 1; use strict; use warnings; use HTML::Parser; my $html = join "\n", "<html>", (map { "<a href=\"$_\">$_</a>" } 1 .. 1000), "</html>"; sub parsing { my ($html) = @_; my $prs = HTML::Parser->new(api_version => 3); # Memory leak $prs->handler(start => sub { $prs->handler(text => sub {}); $prs->handler(end => sub { $prs->handler(text => undef); $prs->handler(end => undef); }); }); # # OK # $prs->handler(start => sub {}); # $prs->handler(text => sub {}); # $prs->handler(end => sub {}); $prs->parse($html); $prs->eof; } print `ps u $$`; # On FreeBSD `ps -u $$` foreach my $i (1 .. 5) { foreach (1 .. 1000) { parsing($html); } print "$i\n"; print `ps u $$`; }
You create circular references when you set up handlers like this.  The callbacks are closures that contain references back to the parser that they are set up to be handler for.

This can be avoided by writing the handlers as:

   $prs->handler('start', sub { my $prs = shift; ... }, 'self');

Look up the documentation of the argspec parameter 'self'.

Regards,
Gisle
Many thanks!!! Nick. Срд Авг 14 14:14:13 2013, GAAS писал: Show quoted text
> You create circular references when you set up handlers like this. The > callbacks are closures that contain references back to the parser that > they are > set up to be handler for. > > This can be avoided by writing the handlers as: > > $prs->handler('start', sub { my $prs = shift; ... }, 'self'); > > Look up the documentation of the argspec parameter 'self'. > > Regards, > Gisle