Have you tried this?  It's sort of equivalent to

}
  my $mw;
  $mw = Win32::GUI::Window->new(
        ...
        -onTerminate => sub { undef $mw },
  );
}

Which uses a closure to stop $mw's ref count going to zero at the end of the enclosing block, but forces it to zero in the onTerminate handler.

Humm - I've just had a bit of a play, and I am not convinced the window destruction logic is correct. Under the hood a window is both an object and a tied hash ref right? Could it be the case that we're destroying one and not the other?

Anyways, have a play with the example below.

Cheers,

jez.
-----------------

use strict;
use warnings;

use Win32::GUI;

# Create the main window
my $mainwindow = new Win32::GUI::Window(
  -name   => "Window",
  -title  => "Objects and Windows",
  -pos    => [100,100],
  -size   => [400,400],
);

#We now create several employee windows
EmployeeSelector->new($mainwindow,20,20);
EmployeeSelector->new($mainwindow,20,50);
EmployeeSelector->new($mainwindow,20,80);
EmployeeSelector->new($mainwindow,20,110);
EmployeeSelector->new($mainwindow,20,140);

$mainwindow->Show();
#Enter the message processing loop
Win32::GUI::Dialog();
exit(0);

package EmployeeSelector;
use strict;
use warnings;
use Win32::GUI;
use base qw(Win32::GUI::Window);

#The constructor
sub new
{
my ($class, $parent, $xcor, $ycor)[EMAIL PROTECTED];

#Create window
my $self = $class->SUPER::new(
#my $self =  new Win32::GUI::Window(
#-parent    => $parent,
-pos       => [$xcor,$ycor],
-size      => [150, 60],
#-onTerminate  => sub {return 1},
#When we terminate, the ref counter of the UserData is dropped, which should kick off the destruction
-onTerminate  => sub {my $self=shift;print "in terminate self is $self\n"},
);
$self->AddButton (
-text   => 'Click Me!',
-pos    => [60,2],
-size  => [60,20],
-onClick => sub {print 'Button Clicked';},
);
print "Created $self\n";
$self->Show();
#Save the window in the window user data, once the sub finishes, there is no reference to the
#window other than itself. The destiny of the window is in it's own hands.
$self->UserData($self);
}

sub DESTROY {
 my $self=shift;
 print "In destroy $self \n";
 $self->SUPER::DESTROY();
}



Reply via email to