Skip to content

Commit

Permalink
Initial implementation. Leak reporting output isn't very helpful just…
Browse files Browse the repository at this point in the history
… yet.
  • Loading branch information
rafl committed Jun 23, 2009
1 parent 5057333 commit c3fe515
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/CatalystX/LeakChecker.pm
@@ -0,0 +1,48 @@
package CatalystX::LeakChecker;

use Moose::Role;
use PadWalker 'closed_over';
use Scalar::Util 'weaken', 'isweak';;
use aliased 'Data::Visitor::Callback', 'Visitor';

use namespace::autoclean -also => [qw/Visitor visit_code/];

sub visit_code {
my ($self, $code, $weak_ctx, $leaks) = @_;
my $vars = closed_over $code;

while (my ($name, $val) = each %{ $vars }) {
next unless $weak_ctx == ${ $val };
next if isweak ${ $val };
push @{ $leaks }, $name;
}

return $code;
}

sub format_table {
my @leaks = @_;

my $t = Text::SimpleTable->new([ 70, 'Variable' ]);
$t->row($_) for @leaks;
return $t->draw;
}

after finalize => sub {
my ($ctx) = @_;
my @leaks;

my $weak_ctx = $ctx;
weaken $weak_ctx;

my $visitor = Visitor->new(
code => sub { visit_code(@_, $weak_ctx, \@leaks) },
);
$visitor->visit($ctx->stash);
return unless @leaks;

my $msg = "Leaked context:\n" . format_table(@leaks);
$ctx->log->debug($msg) if $ctx->debug;
};

1;

0 comments on commit c3fe515

Please sign in to comment.