Perl Programmer/Consultant
Remote System Administrator
Free Software
... contact me
 
  while ($making_other_plans) { life(); }
  location('ipsstb', 'perl_programming', 'curses_ui_notebook_delete_page_bug2');

 For Web Designers 2018-03-25 04:31:58 UTC Mail Delivery Problems? 

Sunday, February 03 2013

Curses::UI::Notebook Page Delete Bug, Part Two

Just a while ago I wrote about a bug in Curses::UI::Notebook delete_page. Well, shucks, I found another. This one will puke up the error The notebook already has a page named... whatever previously used and then deleted name you've chosen.

This bug manifests itself as widget (page) objects not actually being removed from the UI::Curses::Container object by UI::Curses::Notebook. The page/tab disappears but the object itself hangs around, so if you try to reuse the name your application dies. To duplicate:


  #!/usr/bin/perl

  use strict;
  use Curses::UI;

  my $ui = Curses::UI->new();

  my $win = $ui->add('win1', 'Window',
    -border => 0,
    -y      => 0,
    -bfg    => 'blue',
  );

  my $notebook = $win->add(undef, 'Notebook');

  my $p1 = $notebook->add_page('Page 1');
  $p1 -> add(undef, 'Label',
    -x    => 1,
    -y    => 1,
    -text => "This is Page One",
  );

  my $p2 = $notebook->add_page('Page 2');
  $p2 -> add(undef, 'Label',
    -x    => 1,
    -y    => 1,
    -text => "This is Page Two",
  );

  my $foobar = undef;  # keep reading, grasshoppa.

  # Now the fun part:
  $ui->set_timer(
    'i_dont_wanna_dance',
    sub {
        $notebook->delete_page('Page 2');
        undef $p2;
        $foobar = $notebook->add_page('Page 2');    # the name we deleted.
        $foobar->focus();
    },
    5
  );
  # What we expect to happen is that five seconds into runtime, a
  # new page/tab named Page 2 will appear.
  #
  # What ACTUALLY happens is that the application dies and presents
  # us with the last gasp of "The notebook already has a page named
  #'Page 2'!". And for some as-yet unexplored by me reason, the app
  # pins the CPU core it's running on until you AnyKey it. Oh joy.

  $ui->set_binding( sub { $ui->mainloopExit() }, "\cQ" );

  $notebook->focus();
  $ui->mainloop();

Well, at least it's telling the truth. The object isn't visible on the display, but it's still alive in Curses::UI::Container. It's Just Another One Of Those Things That Makes A Guy Say SHIT!

First the workaround:



  $notebook->delete_page('Page 2');
  $notebook->delete('Page 2');           # workaround
  undef $p2;

Isn't that funny? The delete() method is inherited from the parent class Curses::UI::Container so once Curses::UI::Notebook does its little bit of dancing, all it has to do to finish up right is to call $this->delete($page). But that ain't what it does.

Now ya know. At this point, pending further bug discoveries, here's how you delete a page without getting bit:



  $notebook->delete_page('Page 2');
  $notebook->delete('Page 2');          # workaround
  $undef $p2;
  $ui->reset_curses();                  # workaround from previous blog entry

You'll want to do it this way because even if you don't reuse page names, the objects that hang around chew up memory. It's a leak to leave them lying around unusable like that.

Have a happy day!

→ committed: 2/3/2013 01:56:00

[ / perl_programming] permanent link

Comments: 0    Trackbacks: 0

 

Comments are closed for this story.

 

Trackbacks are closed for this story.

Save the Net

Creative Commons License

Project Honeypot Member

 
February 2013
Mon Tue Wed Thu Fri Sat Sun
        3
     

By Month:

By category:

Feeds:

Served to 54.81.117.119:40024 at 04:31:58 GMT on Wednesday, April 25, 2018.

return(0.5105);