don't leak old glob values
Jesse Luehrs [Sat, 13 Nov 2010 17:08:50 +0000 (11:08 -0600)]
Stash.xs
t/20-leaks.t

index 17b9c64..4161ac6 100644 (file)
--- a/Stash.xs
+++ b/Stash.xs
@@ -389,18 +389,28 @@ add_package_symbol(self, variable, initial=NULL, ...)
 
         switch (variable.type) {
         case VAR_SCALAR:
+            if (GvSV(glob))
+                SvREFCNT_dec(GvSV(glob));
             GvSV(glob) = val;
             break;
         case VAR_ARRAY:
+            if (GvAV(glob))
+                SvREFCNT_dec((SV*)GvAV(glob));
             GvAV(glob) = (AV*)val;
             break;
         case VAR_HASH:
+            if (GvHV(glob))
+                SvREFCNT_dec((SV*)GvHV(glob));
             GvHV(glob) = (HV*)val;
             break;
         case VAR_CODE:
+            if (GvCV(glob))
+                SvREFCNT_dec((SV*)GvCV(glob));
             GvCV(glob) = (CV*)val;
             break;
         case VAR_IO:
+            if (GvIO(glob))
+                SvREFCNT_dec((SV*)GvIO(glob));
             GvIOp(glob) = (IO*)val;
             break;
         }
index bdfd37a..53989a6 100644 (file)
@@ -62,7 +62,6 @@ use Symbol;
         $foo->add_package_symbol('$scalar_init' => 1);
         $foo->add_package_symbol('@array_init' => []);
         $foo->add_package_symbol('%hash_init' => {});
-        # hmmm, wonder why this coderef isn't treated as a leak
         $foo->add_package_symbol('&code_init' => sub { "foo" });
         $foo->add_package_symbol('io_init' => Symbol::geniosym);
     } "add_package_symbol doesn't leak";