fix a memory leak introduced by 1f8cad5e5a1875de94d63ac91d8ded4d2282c62e
Christian Walde [Mon, 7 Jul 2014 16:39:33 +0000 (18:39 +0200)]
Changes
lib/Web/Dispatch.pm
t/dispatch_misc.t
t/leak.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index 54613fe..8dc6a01 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
 Revision history for Web-Simple
 
+  - fixes a memory leak that occurs when calling
+    Web::Simple::Application::to_psgi_app
+
 0.024 - 2014-07-03
   - small documentation fixes
 
index 2a5a835..d34fe6e 100644 (file)
@@ -14,7 +14,7 @@ with 'Web::Dispatch::ToApp';
 has dispatch_app => (
   is => 'lazy', builder => sub { shift->dispatch_object->to_app }
 );
-has dispatch_object => (is => 'ro', required => 0);
+has dispatch_object => (is => 'ro', required => 0, weak_ref => 1);
 has parser_class => (
   is => 'ro', default => quote_sub q{ 'Web::Dispatch::Parser' }
 );
index 05bdd67..842cf52 100644 (file)
@@ -64,7 +64,8 @@ sub app_is_object {
         sub to_app { [ 999, [], ["ok"] ] }
     }
 
-    my $d = Web::Dispatch->new( dispatch_object => ObjectApp->new );
+    my $o = ObjectApp->new;
+    my $d = Web::Dispatch->new( dispatch_object => $o );
     my $res = $d->call;
 
     cmp_ok $res->[0], '==', 999, "Web::Dispatch can dispatch properly, given only an object with to_app method";
diff --git a/t/leak.t b/t/leak.t
new file mode 100644 (file)
index 0000000..2f16e1b
--- /dev/null
+++ b/t/leak.t
@@ -0,0 +1,15 @@
+use strictures;
+use Test::More eval { require Devel::Cycle } ? 'no_plan' : ( skip_all => 'No Devel::Cycle' );
+
+use Web::Simple;
+use Devel::Cycle;
+
+my $counter;
+my $on_cycle = sub { Devel::Cycle::_do_report( ++$counter, shift ) };
+{
+    local *STDOUT = *STDERR;
+    Devel::Cycle::find_cycle( main->new->to_psgi_app, $on_cycle );
+}
+ok !$counter, "no leak in to_psgi_app";
+
+done_testing;