I've been hunting through my code, eventually coming to a bug where I had
a closure that referred to itself, as well as an object of mine. This
prevented my object's DESTROY handler from working, causing bugs.

I've been playing with Test::Memory::Cycle, but unfortunately that won't
solve this particular problem.

  my $hash = {};
  $hash->{object} = $some_object;
  $hash->{cycle} = $hash;

No amount of cycle checking in $some_object is going to find this cycle.
If the cycled object is one like this; containing the only reference to
itself, then almost by definition there's nothing that could be walked
for memory cycles.

This is where I consider a refcount-assertion module instead; something I
could do this with:

  use Test::Refcount;

  my $object = Some::Class->new( ... );

  has_onlyref( $object, '$object has only 1 reference' );

  # Synonym for
  has_refcount( $object, 1, '$object has only 1 reference' );

There's a very simple implementation for this I can think of; using:

  use B qw( svref_2object );

  sub refcount {
    my $sv = svref_2object( $_[0] );
    return $sv->REFCNT - 1; # Because @_ refers to it too
  }

Does this sound good?

-- 
Paul "LeoNerd" Evans

[EMAIL PROTECTED]
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/

Attachment: signature.asc
Description: PGP signature

Reply via email to