Dear brothers in perl,

I have a question. I wrote a script which creates old-school html-marque like window with scrolling text. Everything works good, except... huge memory leaks. The amount of leaks depends of length of string, which we pass for displaying. What I've found in Gnome System Monitor is that Heap grows extremely large. It must be some obvious bug in code. I can't find it.

Please help. Usage: ./script_filename <Name of file with string to be displayed>
#!/usr/bin/perl -w use strict; use constant WIDTH => 1000; use constant HEIGHT => 100; use constant SPEED => 15; use Cairo; use Glib qw(TRUE FALSE); use Gtk2 -init; use Gtk2::Pango; my ($text,$ticks); sub draw_text { my ($cr) = @_; my $layout = Gtk2::Pango::Cairo::create_layout($cr); $cr->push_group; $layout->set_text($text); my $desc = Gtk2::Pango::FontDescription -> from_string("Sans Bold +57"); $layout -> set_font_description($desc); $layout->set_alignment("PANGO_ALIGN_LEFT"); Gtk2::Pango::Cairo::update_layout($cr, $layout); my @size = $layout->get_pixel_size; $cr -> translate(-($ticks*SPEED)%$size[0],0); $cr->set_source_rgb(1.0,1.0,1.0); $cr->fill(); $cr -> set_source_rgb(0.0,0.0,0.0); my $count = int(WIDTH/$size[0]); $cr->translate(-$size[0]*2,0); for(my $i = 0; $i<=$count+1; $i++){ $cr->translate($size[0],0); Gtk2::Pango::Cairo::show_layout($cr,$layout); } $cr->pop_group_to_source; $cr->paint; undef $cr; undef $layout; } my $window = Gtk2::Window->new(); $window -> signal_connect(delete_event => sub { Gtk2 -> main_quit(); }); my $area = Gtk2::DrawingArea -> new(); sub draw { my $widget = $area; my $cr = Gtk2::Gdk::Cairo::Context -> create($widget->window()); $cr->rectangle(0,0,WIDTH,HEIGHT); draw_text($cr,$ticks); } sub timer { $ticks ++; draw(); return 1; } $text = 'Please specify your input file'; if($ARGV[0]){ open(INPUT_FILE, "$ARGV[0]"); $text = <INPUT_FILE>; } $ticks = 0; $window -> set_default_size(WIDTH,HEIGHT); $window -> add($area); $window->show_all(); my $timer = Glib::Timeout->add(1000/24,\&timer,'',); Gtk2->main();

In reply to Bad, bad memory leaks in simple script [solved] by Solak

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.