guliver has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I've got an issue with Tk (I presume).

Arhitecture: solaris & hpux.

Perl:

Issue: After calling destroy on a Tk window it hangs.

Code:

$mainwindow = MainWindow->new(); --------- do some Tk GUI stuff --------- $mainwindow->destroy() if (defined $mainwindow); CORE::exit(0);

Output in perl -d (with debbuging output to some log)

...skipping... Init::CODE(0x10dd0d8)(Init.pm:771): Tk::e +xit(0); Tk::Widget::_Destroyed(../perl/lib/sun4-solaris/Tk/Widget.pm:354): ----------------------- some more lines like this one above ----------------------- Tk::Widget::_Destroyed(../perl/lib/sun4-solaris/Tk/Widget.pm:367): 367: delete $w->{$ent}; Tk::Widget::_Destroyed(../perl/lib/sun4-solaris/Tk/Widget.pm:360): 360: my $ent = pop(@$a); Tk::Widget::_Destroyed(../perl/lib/sun4-solaris/Tk/Widget.pm:361): 361: if (ref $ent) 362: { Tk::Widget::_Destroyed(../perl/lib/sun4-solaris/Tk/Widget.pm:367): 367: delete $w->{$ent}; Tk::MainWindow::_Destroyed(../perl/lib/sun4-solaris/Tk/MainWindow.pm:7 +7): 77: delete $Windows{$top}; ------------------------ end of log ------------------------
Does anybody had this issue? Or some idea about what can I try to solve it?

Replies are listed 'Best First'.
Re: Tk::exit hangs
by zentara (Cardinal) on Dec 07, 2006 at 13:35 UTC
    I don't know if there is a specific issue with Solaris or Hpux, but it's not really a bug in general. You just need to understand that the mainwindow, IS the event-loop, and if you destroy the event-loop, all processing stops. This keeps the event-loop going. Does this work?
    #!/usr/bin/perl -w use Tk; #the leakage stops after about 10 open-close cycles #read perldoc Tk::callbacks #this is a better method than the first example my $mw = MainWindow->new(-title=>"Demo"); my $HlpBttn = $mw->Button( -text =>"Make NEW Window", -command => [ \&makeWindow, $mw ], # calls makeWindow and passes $mw as the first argument ); $HlpBttn->pack(-padx =>60, -pady =>10); MainLoop; sub makeWindow { my $parent = shift; my $win = $parent->Toplevel(-title=>'new window'); my $Bttn = $win->Button( -text=>"CLOSE this window", -command=> [ 'destroy', $win ], # calls destroy on $win )->pack( -padx =>60, -pady =>10, ); } __END__
    Here is another script which demonstrates continuation after mainloop
    #!/usr/bin/perl -w use Tk; my $mw = MainWindow->new(-title=>"#1"); $mw->Button(-text=>"Destroy Me", -command=> sub { $mw->destroy })->pack; MainLoop; print "ha ha still going\n"; <>;
    So if you want to exit the program use sub{ exit; }
    exit if defined $mainwindow
    Of course, I'm speaking in generalities, there may very well be some oddity on your system. If you give a working snippet, we can report what we see.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

      The code that uses exit() was working with the previous version of perl we used (5.6.1).

      But after upgrade to perl 5.8.8 I was forced to use CORE::exit(), otherwise perl was crashing.

      If I try to use Tk:exit() (without using $mainwindow->distroy) I get the same behaviour (my script hangs).

      If I try to use CORE::exit() without using $mainwindow->distroy, perl will crash with "Segmentation fault".

      Seems that the main issue with this is the design of the application (big installer with GUI in Tk). The mainwindow and MainLoop are created in a module, the destroy is called in some other modules that import the first one.

      Thanks for suggestions.