in reply to CGI::Application + mod_perl =|!= memory leak

You goofed and forgot to binmode IMAGE or die "Couldn't binmode image". Always do that when working with binary data. You also never close FH. If you meant to just return the contents of the file then you're better off writing that as a plain read - you're forcing perl to jump through hoops.

# Ok, could be better local $/; open ( FH , '/path/to/some/image' ) or confess $!; binmode FH or confess $!; my $image = readline *FH; close FH or confess $!; return $image; # Better open ( FH , '/path/to/some/image' ) or confess $!; binmode FH or confess $!; my $image; read FH, $image, -s FH; close FH or confess $!; return $image; # Best open ( FH , '/path/to/some/image' ) or confess $!; my $image; read FH, $image, -s FH; close FH or confess $!; return \ $image;

Replies are listed 'Best First'.
Re: Re: CGI::Application + mod_perl =|!= memory leak
by submersible_toaster (Chaplain) on May 21, 2003 at 08:24 UTC

    binmode having no use (purportedly) under linux I did not use it. I should have noted that the shortest case foo.pm is really the shortest case, that includes removing the close on the filehandle (which is going out of scope anyway), and also returning a reference to a scalar rather than the scalar itself.

    Sadly, none of these sensible solutions makes the leak go awayl, but I think that returning the reference kept two copies of $image in memory - one copy dissapears once the request in completed.

    diotalevi++
    thankyou

    I can't believe it's not psellchecked
      No, "globbish" filehandles are package variables and never go out of scope. The "best" variant would look like so:
      sub start { warn "## start"; my $self = shift; open my $fh, '<', '/path/to/some/image' or confess $!; $self->header_props(-type=>'your/mimetype'); local $/; return <$fh>; }
      Though that does of course do nothing to solve your leak.

      Makeshifts last the longest.

      If I recall correctly, binmode is something that unix denizens are going to have to use religiously in the future for the perl5 line. So far it isn't a big deal for Linux folk but I seem to recall that around 5.10.x it'll be something you'll need to have done.

      Anyway, its only proper to flag your filehandle so it can handle the right data appropriately.