X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FWeb%2FSimple%2FApplication.pm;h=87d3259d6812ec5221a8a3c65f16bd9b31083630;hb=b5b4423b5305950d881360799ec3e24d138c7d89;hp=4ab18bb7fa4561c8b9f81e6669e3bec8fb8a2fd5;hpb=cf71c76444353953d01b8b81b2d79938dd9609e5;p=catagits%2FWeb-Simple.git diff --git a/lib/Web/Simple/Application.pm b/lib/Web/Simple/Application.pm index 4ab18bb..87d3259 100644 --- a/lib/Web/Simple/Application.pm +++ b/lib/Web/Simple/Application.pm @@ -25,10 +25,22 @@ sub _build__dispatcher { require Web::Dispatch; require Web::Simple::DispatchNode; my $final = $self->_build_final_dispatcher; + + # We need to weaken both the copy of $self that the + # app parameter will close over and the copy that'll + # be passed through as a node argument. + # + # To ensure that this doesn't then result in us being + # DESTROYed unexpectedly early, our to_psgi_app method + # closes back over $self + + weaken($self); + my $node_args = { app_object => $self }; + weaken($node_args->{app_object}); Web::Dispatch->new( app => sub { $self->dispatch_request(@_), $final }, node_class => 'Web::Simple::DispatchNode', - node_args => { app_object => $self } + node_args => $node_args ); } @@ -57,7 +69,16 @@ sub _run_fcgi { sub to_psgi_app { my $self = ref($_[0]) ? $_[0] : $_[0]->new; - $self->_dispatcher->to_app; + my $app = $self->_dispatcher->to_app; + + # Close over $self to keep $self alive even though + # we weakened the copies the dispatcher has; the + # if 0 causes the ops to be optimised away to + # minimise the performance impact and avoid void + # context warnings while still doing the closing + # over part. As Mithaldu said: "Gnarly." ... + + return sub { $self if 0; goto &$app; }; } sub run {