Toby Inkster avatar Toby Inkster committed 70b12fb

fix AttributeHash mem leak, using Scalar::Util::weaken

Comments (0)

Files changed (2)

 use Carp;
 use overload
-    '%{}'  => 'getAttributesHash',
+    '%{}'  => 'getAttributeHash',
     'bool' => sub { 1 },
     # Perl 5.8, but XML-LibXML already dropped support for older
     # Perls since XML-LibXML-1.77.
     use Hash::FieldHash qw();
+    use Scalar::Util qw();
     my %tiecache;
     BEGIN { Hash::FieldHash::fieldhash(%tiecache) };
-    sub getAttributesHash {
+    sub getAttributeHash {
         my $self = shift;
         if (!exists $tiecache{ $self }) {
-            tie my %attr, 'XML::LibXML::AttributeHash', $self;
+            tie my %attr, 'XML::LibXML::AttributeHash', $self, weaken => 1;
             $tiecache{ $self } = \%attr;
+            Scalar::Util::weaken($tiecache{ $self });
         return $tiecache{ $self };


 use strict;
 use warnings;
+use Scalar::Util qw//;
 use Tie::Hash;
 our @ISA = qw/Tie::Hash/;
 sub TIEHASH {
-    my ($class, $element) = @_;
-    bless [$element], $class;
+    my ($class, $element, %args) = @_;
+    if ($args{weaken}) {
+        Scalar::Util::weaken($element);
+    }
+    bless [$element, undef, \%args], $class;
 sub STORE {
  $element->{'href'} = '';
  print $element->getAttribute('href') . "\n";
+The tie interface allows the passing of additional arguments to
+ tie my %hash, 'XML::LibXML::AttributeHash', $element, %args;
+Currently only one argument is supported, the boolean "weaken" which (if
+true) indicates that the tied object's reference to the element should be
+a weak reference. This is used by XML::LibXML::Element's overloading.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.