Test::Weaken

Test that freed memory objects were, indeed, freed
Download

Test::Weaken Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Jeffrey Kegler
  • Publisher web site:
  • http://search.cpan.org/~jkegl/

Test::Weaken Tags


Test::Weaken Description

Test that freed memory objects were, indeed, freed A memory leak occurs when a Perl data structure is destroyed but some of the contents of that structure are not freed. Leaked memory is a useless overhead. Leaks can significantly impact system performance. They can also cause an application to abend due to lack of memory.In Perl, circular references are a common cause of memory leaks. Circular references are allowed in Perl, but data structures containing circular references will leak memory unless the programmer takes specific measures to prevent leaks. Preventive measures include weakening the references and arranging to break the reference cycle just before the structure is destroyed.When using circular references, it is easy to misdesign or misimplement a scheme for preventing memory leaks. Mistakes of this kind have been hard to detect in a test suite.Test::Weaken is a Perl module that allows easy detection of unfreed Perl data. Test::Weaken allows you to examine the unfreed data, even data that would usually have been made inaccessible.Test::Weaken frees the test structure, then looks to see if any of the contents of the structure were not actually deallocated. By default, Test::Weaken determines the contents of a data structure by examining arrays and hashes, by following references, and by following tied variables to their underlying object. Test::Weaken does this recursively to unlimited depth.Test::Weaken can deal with circular references without going into infinite loops. Test::Weaken will not visit the same Perl data object twice.SYNOPSIS use Test::Weaken qw(leaks); use Data::Dumper; use Math::BigInt; use Math::BigFloat; use Carp; use English qw( -no_match_vars ); my $good_test = sub { my $obj1 = Math::BigInt->new('42'); my $obj2 = Math::BigFloat->new('7.11'); ; }; if ( !leaks($good_test) ) { print "No leaks in test 1\n" or Carp::croak("Cannot print to STDOUT: $ERRNO"); } else { print "There were memory leaks from test 1!\n" or Carp::croak("Cannot print to STDOUT: $ERRNO"); } my $bad_test = sub { my $array = ; push @{$array}, $array; $array; }; my $bad_destructor = sub {'I am useless'}; my $tester = Test::Weaken::leaks( { constructor => $bad_test, destructor => $bad_destructor, } ); if ($tester) { my $unfreed_proberefs = $tester->unfreed_proberefs(); my $unfreed_count = @{$unfreed_proberefs}; printf "Test 2: %d of %d original references were not freed\n", $tester->unfreed_count(), $tester->probe_count() or Carp::croak("Cannot print to STDOUT: $ERRNO"); print "These are the probe references to the unfreed objects:\n" or Carp::croak("Cannot print to STDOUT: $ERRNO"); for my $ix ( 0 .. $#{$unfreed_proberefs} ) { print Data::Dumper->Dump( ], ) or Carp::croak("Cannot print to STDOUT: $ERRNO"); } } Requirements: · Perl


Test::Weaken Related Software