From: Vincent Pit Date: Mon, 30 Nov 2009 20:18:23 +0000 (+0100) Subject: Stop leaking objects stored in the data slot X-Git-Tag: v0.39~6 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FVariable-Magic.git;a=commitdiff_plain;h=068ea108c28a60581d00b44976dc979860832c8e Stop leaking objects stored in the data slot --- diff --git a/MANIFEST b/MANIFEST index d4d24e8..88af47a 100644 --- a/MANIFEST +++ b/MANIFEST @@ -37,6 +37,7 @@ t/34-glob.t t/35-stash.t t/40-threads.t t/41-clone.t +t/80-leaks.t t/91-pod.t t/92-pod-coverage.t t/95-portability-files.t diff --git a/Magic.xs b/Magic.xs index 6ec752b..df86b33 100644 --- a/Magic.xs +++ b/Magic.xs @@ -681,6 +681,7 @@ STATIC UV vmg_cast(pTHX_ SV *sv, const SV *wiz, SV **args, I32 items) { data = (w->cb_data) ? vmg_data_new(w->cb_data, sv, args, items) : NULL; mg = sv_magicext(sv, data, PERL_MAGIC_ext, w->vtbl, (const char *) wiz, HEf_SVKEY); + SvREFCNT_dec(data); mg->mg_private = SIG_WIZ; #if MGf_COPY if (w->cb_copy) diff --git a/t/80-leaks.t b/t/80-leaks.t new file mode 100644 index 0000000..dcaf66a --- /dev/null +++ b/t/80-leaks.t @@ -0,0 +1,40 @@ +#!perl -T + +use strict; +use warnings; + +use Test::More tests => 3; + +use Variable::Magic qw/wizard cast/; + +our $destroyed; + +{ + package Variable::Magic::TestDestructor; + + sub new { bless { }, shift } + + sub DESTROY { ++$::destroyed } +} + +sub D () { 'Variable::Magic::TestDestructor' } + +{ + local $destroyed = 0; + + my $w = wizard data => sub { $_[1] }; + + { + my $obj = D->new; + + { + my $x = 1; + cast $x, $w, $obj; + is $destroyed, 0; + } + + is $destroyed, 0; + } + + is $destroyed, 1; +}