Gdk Pixbufloader not freeing memory



I had a problem using the pixbufloader with Gtkperl.  Basically the loader
object doesn't seem to be releasing memory even when it is explicitly undef'd.
I wrote a small test script to try to isolate the problem.  It creates a window
with a single button.  Every time you click the button an image (hard coded in
the script) will be loaded using the pixbufloader.  If I run this program and
watch the memory usage with top, I can see that the memory utilization for the
script just keeps increasing.  Has anyone seen this before?  Is there a
workaround?


#!/usr/bin/env perl
use Gtk::Gdk::Pixbuf;
use Gtk;
use IO::File;
use warnings;
use strict;

# Set the name of the file to load
my $file="/home/jforkey/temp/pictures/IMG_1242.JPG";

my $loader;

Gtk->init();

# Make the GUI
my $Win = Gtk::Window->new;
my $box = Gtk::HBox->new(0,0);
my $button1 = Gtk::Button->new("Load image");
$box->pack_start($button1, 0, 0, 0);
$button1->signal_connect("clicked", \&load_image, $file);
$Win->add($box);
$Win->show_all;
$Win->signal_connect("delete_event", \&Gtk::main_quit);

Gtk->main();

# load 1k bytes from image file
sub load_image_buf {
  my ($fh) = @_;
  my $buf;

  my $NumRead = sysread($fh,$buf, 1024);
  defined $NumRead or die "couldn\'t read\n";
  if ($NumRead == 1024) {
    print "Writing $NumRead bytes to pixbuf loader\n";
    $loader->write($buf);
    return 1; # leave function on the Gtk Idle execution loop
  }
  else {
    # last chunk of data from image
    print "Writing $NumRead bytes to pixbuf loader\n";
    $NumRead and $loader->write($buf);
    print "closing pixbuf loader\n";
    $loader->close;
    $loader = undef; # *** memory should be released here ***
    $fh->close;
    return 0; # take function off the Gtk Idle execution loop
  }
}

# Set up pixbufloader to load image.
sub load_image {
  shift; # get rid of Gtk::Button object
  my ($name) = @_;
  
  # if we're already loading the image don't do anything
  $loader or do {
    # open file handle for image    
    my $fh= IO::File->new($name, "r");
    $loader= Gtk::Gdk::PixbufLoader->new;
    # every 100 ms execute load_image_buf to load another 1k bytes of the image
    Gtk->idle_add(\&load_image_buf, $fh);
  }
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]