Subject: | memory leak when using weak references |
Hi.
I'm too tired to describe the problem, so I hope a test and draft of a
patch will help you to figure out what's wrong and fix it in the right way.
--
Best regards, Ruslan.
Subject: | clone_memory_leak.pl |
use strict;
use warnings;
package foo;
sub DESTROY { print "del $_[0]\n" }
package main;
use Clone qw(clone);
use Scalar::Util qw(weaken isweak);
{
my $a = bless { }, 'foo';
print "new $a\n";
my $b = bless { r => $a }, 'foo';
print "new $b\n";
$a->{'r'} = $b;
weaken($b->{'r'});
my $c = clone($a);
print "new $c\n";
}
print "The End.\n";
Subject: | clone_memory_leak_draft.patch |
ТолÑко в Clone-0.23-my/: blib
ТолÑко в Clone-0.23-my/: Clone.bs
ТолÑко в Clone-0.23-my/: Clone.c
ТолÑко в Clone-0.23-my/: Clone.o
diff -ru Clone-0.23/Clone.xs Clone-0.23-my/Clone.xs
--- Clone-0.23/Clone.xs 2007-04-20 09:40:27.000000000 +0400
+++ Clone-0.23-my/Clone.xs 2007-06-21 04:12:43.000000000 +0400
@@ -123,7 +123,7 @@
{
SV *clone = ref;
SV **seen = NULL;
- UV visible = (SvREFCNT(ref) > 1);
+ UV visible = (SvREFCNT(ref) > 1) || (SvMAGICAL(ref) && mg_find(ref, '<'));
int magic_ref = 0;
TRACEME(("ref = 0x%x(%d)\n", ref, SvREFCNT(ref)));
@@ -226,6 +226,7 @@
for (mg = SvMAGIC(ref); mg; mg = mg->mg_moremagic)
{
SV *obj = (SV *) NULL;
+ int magic_clone = 1;
/* we don't want to clone a qr (regexp) object */
/* there are probably other types as well ... */
TRACEME(("magic type: %c\n", mg->mg_type));
@@ -241,6 +242,7 @@
continue;
break;
case '<': /* PERL_MAGIC_backref */
+ magic_clone = 0;
continue;
break;
default:
@@ -251,6 +253,7 @@
}
magic_ref++;
/* this is plain old magic, so do the same thing */
+ if (magic_clone)
sv_magic(clone,
obj,
mg->mg_type,
@@ -280,6 +283,9 @@
{
sv_bless (clone, SvSTASH (SvRV (ref)));
}
+ if (SvWEAKREF(ref)) {
+ sv_rvweaken(clone);
+ }
}
TRACEME(("clone = 0x%x(%d)\n", clone, SvREFCNT(clone)));
ТолÑко в Clone-0.23-my/: Makefile
ТолÑко в Clone-0.23-my/: Makefile.old
diff -ru Clone-0.23/Makefile.PL Clone-0.23-my/Makefile.PL
--- Clone-0.23/Makefile.PL 2006-10-08 07:37:20.000000000 +0400
+++ Clone-0.23-my/Makefile.PL 2007-06-21 04:04:54.000000000 +0400
@@ -6,7 +6,7 @@
'NAME' => 'Clone',
'VERSION_FROM' => 'Clone.pm', # finds $VERSION
'LIBS' => [''], # e.g., '-lm'
- 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING'
+# 'DEFINE' => '-DDEBUG_CLONE', # e.g., '-DHAVE_SOMETHING'
'INC' => '', # e.g., '-I/usr/include/other'
# 'OPTIMIZE' => '-g', # e.g., '-I/usr/include/other'
'OPTIMIZE' => '-O3', # e.g., '-I/usr/include/other'
ТолÑко в Clone-0.23-my/: pm_to_blib