Front page | perl.perl5.porters |
Postings from June 2009
Re: DESTROY: is there any predictability about when it's called?
Thread Previous
|
Thread Next
From:
demerphq
Date:
June 14, 2009 03:09
Subject:
Re: DESTROY: is there any predictability about when it's called?
Message ID:
9b18b3110906140308h7ede5e5cn332f8da2b0f35c22@mail.gmail.com
2009/6/14 demerphq <demerphq@gmail.com>:
> 2009/6/13 Andy Armstrong <andy@hexten.net>:
>> I'm writing some code that catches memory leaks:
>>
>> leakguard {
>> # do something that leaks
>> };
>>
>> If there are any object leaks inside the block it should warn at exit. The
>> leakguard sub looks like this:
>>
>> sub leakguard(&@) {
>> my $block = shift;
>> my $state = My::Mem::State->new( @_ );
>> my $rc = $block->();
>> my ( undef ) = ( $state );
>> return $rc;
>> }
>>
>> My::Mem::State checks for leaked blessed objects when its DESTROY method is
>> called. The problem is that it gets false positives from objects allocated
>> inside $block for which DESTROY is pending but get DESTROYed /after/ the
>> My::Mem::State.
>>
>> On 5.10 the my ( undef ) = ( $state ) seems to work to force $state to be
>> destroyed late enough to avoid false positives - but that's a bit heuristic
>> for my tastes. Is there a reliable way to flush pending DESTROYs or some
>> other way to guarantee that $state is lives longer than anything allocated
>> by $block?
>
> I dont really understand this code. The correct way to tell perl to
> free memory in a block is to explicitly undef the variable.
>
And i mean this:
undef $x;
not this:
$x= undef;
the two are conceptually different, with the first explicitly freeing
the memory, the second does not.
demerphq@gemini:~$ perl -MDevel::Peek -e'for (reverse 1..10) { my $x;
warn "len: ", defined $x ? length($x): "undef","\n"; Dump($x); $x="x"
x $_; $x=undef }'
len: undef
SV = NULL(0x0) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0x816cb00 "xxxxxxxxxx"\0
CUR = 10
LEN = 12
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0x816cb00 "xxxxxxxxx"\0
CUR = 9
LEN = 12
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0x816cb00 "xxxxxxxx"\0
CUR = 8
LEN = 12
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0x816cb00 "xxxxxxx"\0
CUR = 7
LEN = 12
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0x816cb00 "xxxxxx"\0
CUR = 6
LEN = 12
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0x816cb00 "xxxxx"\0
CUR = 5
LEN = 12
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0x816cb00 "xxxx"\0
CUR = 4
LEN = 12
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0x816cb00 "xxx"\0
CUR = 3
LEN = 12
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0x816cb00 "xx"\0
CUR = 2
LEN = 12
demerphq@gemini:~$ perl -MDevel::Peek -e'for (reverse 1..10) { my $x;
warn "len: ", defined $x ? length($x): "undef","\n"; Dump($x); $x="x"
x $_; undef $x }'
len: undef
SV = NULL(0x0) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0
len: undef
SV = PV(0x8154ae8) at 0x8153cdc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
PV = 0
--
perl -Mre=debug -e "/just|another|perl|hacker/"
Thread Previous
|
Thread Next