AE::HTTP http_get with want_body_handle doesn't work unless either an on_eof or on_error callback is defined

Marc Lehmann schmorp at schmorp.de
Wed Jan 11 08:04:06 CET 2017


On Mon, Jan 09, 2017 at 06:36:05PM +0100, Peter Juhasz <pjuhasz at uhusystems.com> wrote:
> 	sub {
> 		my $handle = shift;

$handle is a local variable, it will go out of scope as soon as the sub
returns.

> 		$handle->on_error(sub {
> 			undef $handle;

this closure captures $handle, and so creates a circular reference which
keeps the value in $handle alive ($handle references the callback, the
callback references the handle).

> Question: Is this a bug in AE::HTTP (or AE::Handle), or is something
> wrong with the way I'm using it?

The latter - perl cleans up objects automatically that are not referenced
anymore. Without your on_error/on_eof, nothing keeps it alive, so it goes
out of scope the instant the sub returns.

To view it differently, your sub, without on_error/on_eof, doesn't
actually use the handle anymore once it returns, so there would be no way
to reach it from your code anymore, so it couldn't be used to receive data
anymore either.

-- 
                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at schmorp.de
      -=====/_/_//_/\_,_/ /_/\_\



More information about the anyevent mailing list