[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Memory Leak workaround!



Thanks to Alan Fry's MacTE.pm I have been able to fixup my
"Input/Output" file prompter.
(The first version used MacDialog which has horrific memory leaks...)
The following version
is only a few minutes old :) and needs some polishing...Pay attention to
the initial comments,
it appears to be *much* kinder to memory.

Thanks to all who offered suggestions.

Jerry
levan@eagle.eku.edu
#!perl -w

# A framework for 1) selecting a file for input,
#                 2) selecting a  file for output,
#                 3) performing a task involving the two files.
#
# Author: Jerry LeVan <mailto:levan@eagle.eku.edu>,
<http://eagle.eku.edu:4000/>
#
# Environment: MacPerl 5.2.1a1 + MacOS 9.04 +
#       MacTE.pm from Alan Fry <mailto:ajf@afco.demon.co.uk>
#                              <http://www.afco.demon.co.uk>
# Date: Aug 15, 2000
# Version 1.0 alpha
#
# Bugs: The warn flag generates a number of warnings involving edit and
focus.
#       The Dispose code generates a number of:
#        "Use of uninitialized value during global destruction"
#        notices.
#        I think the MacTE.pm does not hand <cr> properly. If the
#        purpose of the MacTE.pm module is to simulate one line edit
#        boxes then the code needs a fix ( cuz <cr>'s are stored ).

use Mac::MacTE;
use Mac::Events;
use Mac::Controls;
use Mac::QuickDraw;
use Mac::Windows;
use Mac::StandardFile;
use strict;

my ($infilename,$outfilename);    # source and destination files for
processing

# Main window rectangle
my $rect = Rect->new(350,280,775,486);

my $dlg = MacColorWindow->new($rect, 'Translator',1,4,1);

$dlg->sethook('drawgrowicon',sub {1});
SetPort($dlg->window);

my $fonts = {font => 2002,face => 0} ;

   $fonts = { font => 0, face => 0 };

# two static text labels
my $st_1 = Mac::MacTE->new($dlg,Rect->new(22,15,163,37),'Select Input
File:',0,$fonts);
my $st_2 = Mac::MacTE->new($dlg,Rect->new(22,95,163,117),'Select Output
File:',0,$fonts);

# two edit text items
my $et_1 = Mac::MacTE->new($dlg,Rect->new(30,50,320,72),'',1,$fonts);
#input file
my $et_2 = Mac::MacTE->new($dlg,Rect->new(30,120,320,142),'',1,$fonts);
# output file

# Two browse buttons
# this button triggers the translation
my $exec = $dlg->new_control(Rect->new(31,167,89,187),  # rectangle
                             "Execute",       # label
        1,               # visible
        0,0,0,           # default, min and max values
        pushButProc()    #  a button
     );
 $exec->sethook('hit',\&do_task);      # the whole purpose of this
gui...

 # This button cancels everything
 my $cancel = $dlg->new_control(Rect->new(189,167,247,187),  # rectangle

                             "Cancel",       # label
        1,               # visible
        0,0,0,           # default, min and max values
        pushButProc()    #  a button
     );
 $cancel->sethook('hit',\&quit_program);

 # invoke the input file selector
 my $inbrowse = $dlg->new_control(Rect->new(342,52,400,70),  # rectangle

                             "Browse",       # label
        1,               # visible
        0,0,0,           # default, min and max values
        pushButProc()    #  a button
     );
 $inbrowse->sethook('hit' => \&getfile );

 my $outbrowse = $dlg->new_control(Rect->new(342,122,400,140),  #
rectangle
                             "Browse",       # label
        1,               # visible
        0,0,0,           # default, min and max values
        pushButProc()    #  a button
     );
 $outbrowse->sethook('hit' => \&putfile);

WaitNextEvent while $dlg->window;

# This sub will load the user specified file into the Text Edit field
sub getfile {
  if ($_[1] == kControlButtonPart() ) {
    my $file =StandardGetFile(0,'TEXT');
        if ( $file->sfGood()) {
              $et_1->set_text($file->sfFile());
         }
   }
   return 1;
}

# This sub will load the user specified output file into the Text Edit
Field
sub putfile {
  if ( $_[1] == kControlButtonPart()) {
    my $file = StandardPutFile('Output File Name: ',"");
    if($file->sfGood()) {
         $et_2->set_text($file->sfFile());
     }
   }
   return 1;
}

sub quit_program {
  if ( $_[1] == kControlButtonPart()) {
     $dlg->dispose();
     exit(1);
   }
   return 1;
}

sub do_task {
  if ( $_[1] == kControlButtonPart()) {
    # check for an input file...
    $infilename = $et_1->get_text;
    if (length($infilename) <= 0) {
       MacPerl::Answer("You did not specify an input file.");
       return 1;
    }
    $outfilename = $et_2->get_text;
    if (length($outfilename) <= 0 ) {
       MacPerl::Answer("You did not specify an output file.");
       return 1;
    }
    print "Processing $infilename to produce $outfilename\n" ;
    $dlg->dispose();
  }
 return 1;
}

END {
       $dlg->dispose() if ( defined($dlg));
    }


# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org