//These are the global variables
var
AppForm: TAppForm;
Reg0,Reg1,Reg2,Reg3,Reg4,Lst,Hrs,Hms: double; // reg0 is for swapping display stack
Mem1,Mem2,Mem3,Mem4,Mem5,Mem6,Mem7,Mem8,Mem9: double;
Err,Dsp: integer;
TmpStr,LstDsp,PrgmFolder: string;
NewReg,SavReg,StoActive,HrsMode: boolean;
//This is the function that creates an onclose crash.
//If I exclude this call the program does not crash at OnClose.
//Debug indicates that it is Str(Reg1:0:Dsp,TmpStr) ... it is as if Reg1 has junk data in it.
//But I don't understand why UpdDsp is firing at OnClose to begin with.
procedure TAppForm.UpdDsp;
var x,z:integer;
begin
try
Str(Reg1:0:Dsp,TmpStr); // *** crash point at OnClose ***
x:= pos('.',TmpStr);
z:= length(TmpStr);
if (x>17)or(z>17) then // this only fires when a number is very large ... large numbers are not the problem.
begin
TmpStr:= FloatToStrF(Reg1,ffExponent,17,0); //showmessage(TmpStr);
end;
RegBox1.text:= TmpStr;
finally
end;
try
Str(Reg2:0:Dsp,TmpStr);
x:= pos('.',TmpStr);
z:= length(TmpStr);
if (x>17)or(z>17) then
begin
TmpStr:= FloatToStrF(Reg2,ffExponent,17,0); //showmessage(TmpStr);
end;
RegBox2.text:= TmpStr;
finally
end;
try
Str(Reg3:0:Dsp,TmpStr);
x:= pos('.',TmpStr);
z:= length(TmpStr);
if (x>17)or(z>17) then
begin
TmpStr:= FloatToStrF(Reg3,ffExponent,17,0); //showmessage(TmpStr);
end;
RegBox3.text:= TmpStr;
finally
end;
try
Str(Reg4:0:Dsp,TmpStr);
x:= pos('.',TmpStr);
z:= length(TmpStr);
if (x>17)or(z>17) then
begin
TmpStr:= FloatToStrF(Reg4,ffExponent,17,0); //showmessage(TmpStr);
end;
RegBox4.text:= TmpStr;
finally
end;
ResetFocus;
Update;
end;
//I use this exception handler which writes to a log file.
//It does not activate at OnClose, so the crash is not the program.
//Yet debug is indicating that the crash is on account of Str(Reg1:0:Dsp,TmpStr).
//It is as if there are some trashed functions or variables that are not being purged at OnClose.
procedure TAppForm.LogException(Sender: TObject; E: Exception);
var ErrorFile: TextFile;
ErrorInfo: String;
AppPath,SenderMsg: string;
begin
SenderMsg:= E.Message; // e.message contains line-feed
AppPath:= ExtractFilePath(ParamStr(0));
AssignFile(ErrorFile, AppPath+'CmdRPN.log');
if FileExists(AppPath+'CmdRPN.log')
then Append(ErrorFile)
else Rewrite(ErrorFile);
ErrorInfo:= DateTimeToStr(Now)+' Unhandled exception';
if Assigned(Sender)
then ErrorInfo:= ErrorInfo+' in '+Sender.UnitName+'->'+Sender.ClassName;
ErrorInfo:= ErrorInfo+': '+SenderMsg;
try WriteLn(ErrorFile, ErrorInfo);
finally
CloseFile(ErrorFile);
ShowMessage('Application fault... message saved: CmdRPN.log');
end;
end;
//These are the Close functions that I employ ... there is no call to UpdDsp in them.
procedure TAppForm.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
CloseAction:= caFree;
end;
procedure TAppForm.FormCloseQuery(Sender: TObject; var CanClose: boolean);
var CfgFile: TextFile;
DatStr: string;
OK: boolean; // DatOK: boolean;
IOval: integer; // longint;
begin
AssignFile(CfgFile, PrgmFolder+'CmdRPN.cfg');
{$I-} Rewrite(CfgFile) {$I+};
IOval:=IOresult; OK:=(IOval=0);
if OK then // was DatOK
begin try
Str(Reg0,DatStr); Writeln(CfgFile,DatStr); // register display
Str(Reg1,DatStr); Writeln(CfgFile,DatStr);
Str(Reg2,DatStr); Writeln(CfgFile,DatStr);
Str(Reg3,DatStr); Writeln(CfgFile,DatStr);
Str(Reg4,DatStr); Writeln(CfgFile,DatStr);
Str(Mem1,DatStr); Writeln(CfgFile,DatStr); // storage memory
Str(Mem2,DatStr); Writeln(CfgFile,DatStr);
Str(Mem3,DatStr); Writeln(CfgFile,DatStr);
Str(Mem4,DatStr); Writeln(CfgFile,DatStr);
Str(Mem5,DatStr); Writeln(CfgFile,DatStr);
Str(Mem6,DatStr); Writeln(CfgFile,DatStr);
Str(Mem7,DatStr); Writeln(CfgFile,DatStr);
Str(Mem8,DatStr); Writeln(CfgFile,DatStr);
Str(Mem9,DatStr); Writeln(CfgFile,DatStr);
Str(Dsp,DatStr); Writeln(CfgFile,DatStr); // decimal display
if HrsMode=true then DatStr:='1' // HmsMode ...Dms
else DatStr:='0'; // HrsMode ...Dec
Writeln(CfgFile,DatStr); // angle mode
finally
Flush(CfgFile);
CloseFile(CfgFile);
CanClose:= true; //showmessage('closing by CanClose'); // good
end; // end try
end else begin
CloseFile(CfgFile);
CanClose:= false;
end;
CanClose:= true; //showmessage('closing by CloseQuery'); // good
end;