Skip Menu |

This queue is for tickets about the Parse-RecDescent CPAN distribution.

Report information
The Basics
Id: 86026
Status: resolved
Priority: 0/
Queue: Parse-RecDescent

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

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



Subject: __ACTIONn__ key in parse tree seems to cause a memory leak
The __ACTIONn__ key in the output parse tree seems to create a circular reference and subsequently causes a memory leak. use Parse::RecDescent; use Data::Dumper; use Test::Memory::Cycle; my $grammar = q( startrule : day month date day : "Sat" | "Sun" | "Mon" | "Tue" | "Wed" | "Thu" | "Fri" month : "Jan" | "Feb" | "Mar" | "Apr" | "May" | "Jun" | "Jul" | "Aug" | "Sep" | "Oct" | "Nov" | "Dec" date: /\d+/ { $item{'INT'} = bless( {'__VALUE__' => $item[1]}, 'INT' ); bless \%item, $item[0]; } ); my $parser = Parse::RecDescent->new($grammar); my $tree = $parser->startrule("Sun Mar 31"); print Dumper($tree); memory_cycle_ok($tree); The result is: $VAR1 = bless( { '__RULE__' => 'date', '__ACTION1__' => $VAR1, 'DATE' => $VAR1, 'INT' => bless( { '__VALUE__' => '31' }, 'INT' ), '__PATTERN1__' => '31' }, 'date' ); not ok 1 # Failed test at ... line 22. # Cycle #1 # date A->{__ACTION1__} => date A # Tests were run but no plan was declared and done_testing() was not seen.
Subject: Re: [rt.cpan.org #86026] __ACTIONn__ key in parse tree seems to cause a memory leak
Date: Tue, 11 Jun 2013 14:18:56 +1000
To: bug-Parse-RecDescent [...] rt.cpan.org
From: Damian Conway <damian [...] conway.org>
Hi Trystan, This is not a bug. The code is doing exactly what you specified. The %item hash contains the results of all components of the 'date' rule, including the action inside which you bless %item itself. And because bless returns its first argument (i.e. a reference to %item itself), that hashref then gets stored back inside %item, and therefore "inside itself" as it were. To solve the memory leak you need to not return a reference to the same hash that's going to be storing the reference you return (%-). Like so: date: /\d+/ { $item{'INT'} = bless( {'__VALUE__' => $item[1]}, 'INT' ); bless {%item}, $item[0]; } This version decants the contents of %item into a new anonymous hash, which breaks the self-reference loop you created. Hope this helps, Damian
On Tue Jun 11 00:19:50 2013, damian@conway.org wrote: Show quoted text
> Hi Trystan, > > This is not a bug. The code is doing exactly what you specified. > > The %item hash contains the results of all components of the > 'date' rule, including the action inside which you bless %item itself. > > And because bless returns its first argument (i.e. a reference to %item > itself), that hashref then gets stored back inside %item, and therefore > "inside itself" as it were. > > To solve the memory leak you need to not return a reference > to the same hash that's going to be storing the reference you return (%-). > > Like so: > > date: > /\d+/ > { > $item{'INT'} = bless( {'__VALUE__' => $item[1]}, 'INT' ); > bless {%item}, $item[0]; > } > > This version decants the contents of %item into a new anonymous hash, > which breaks the self-reference loop you created. > > Hope this helps, > > Damian
Thanks very much. That is a big help. I was stumped. I am using this action to insert "imaginary tokens" in the parse tree. I had borrowed the action code "{ bless \%item, $item[0] }" in the autotree documentation, but bless {%item} is working perfectly. Thanks again, Trystan
Subject: Re: [rt.cpan.org #86026] __ACTIONn__ key in parse tree seems to cause a memory leak
Date: Wed, 12 Jun 2013 07:30:35 -0700
To: bug-Parse-RecDescent [...] rt.cpan.org
From: jtbraun [...] cpan.org
Thanks Damian! On 6/10/2013 9:19 PM, damian@conway.org via RT wrote: Show quoted text
> This version decants the contents of %item into a new anonymous hash, > which breaks the self-reference loop you created. > > Hope this helps, > > Damian >