Recent

Author Topic: Windows 10 and FormClose  (Read 22819 times)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Windows 10 and FormClose
« Reply #30 on: July 02, 2019, 04:16:37 pm »
@wp
Quote
Why are you using empty finally blocks in try-finally-end? This is absolutely useless.
Because the editor automatically kicks the 'finally' in. I leave them because I might need them later. They do any harm.
The editor adds finally only if you introduce "try" yourself.
Such redundant blocks will harm performance-critical sections of code unnecessarily. There is a small speed penalty introduced with each "try" block.

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Windows 10 and FormClose
« Reply #31 on: July 02, 2019, 08:57:06 pm »
@howardpc

I did not have the 'try' implemented earlier. I added it because debug was saying that the crash was on Str() and it choked on value that was too large or small. After implementing the 'try' it did not change the event. The math was with normal numbers ... 22, 55, 66 etc. The math processed correctly. Only when I closed the program had the crash occurred. The application disappeared and the exception notice popped up.

I also notice that it usually happens when I have used the keyboard to enter values. I have numeral buttons on the GUI. If I use them I don't get a crash.

========================
These are typical for the GUI entry...

procedure TAppForm.Num2BtnClick(Sender: TObject);
begin
ChkReg;
RegBox1.Text:= RegBox1.Text+'2';
ResetFocus;
end;

procedure TAppForm.PlusBtnClick(Sender: TObject);
begin
Lst:=Reg1;
Reg1:=Reg1+Reg2; Reg2:=Reg3; Reg3:=Reg4; Reg4:=0;
UpdDsp; NewReg:=true; SavReg:=true;
end;

==========================
These are typical for keyboard entries...

procedure TAppForm.RegBox1KeyPress(Sender: TObject; var Key: Char);
var KeyOK:boolean;
    x:integer;
begin
KeyOK:=false;
if (Key in ['0'..'9'])or(key='.') then  //digit keys
   begin
   KeyOK:=true; ChkReg; //PutKey(Key);
   RegBox1.Text:= RegBox1.Text+Key;
   X:=Pos('.',RegBox1.Text);
   if x=1 then RegBox1.Text:= '0'+RegBox1.Text;
   ResetFocus; Key:=#0;
   end;
if Key=#13 then   //enter key
   begin
   if RegBox1.Text='' then RegBox1.Text:='0';
   Lst:=Reg1;
   KeyOK:=true; UpdReg; UpdDsp; NewReg:=true; SavReg:=false; Key:=#0;
   end;
if Key='+' then   //plus key
   begin
   Lst:=Reg1;
   Reg1:=Reg1+Reg2; Reg2:=Reg3; Reg3:=Reg4; Reg4:=0; Key:=#0;
   KeyOK:=true; UpdDsp; NewReg:=true; SavReg:=true;
   end;
if Key='-' then    //minus key
   begin
   Lst:=Reg1;
   Reg1:=Reg2-Reg1; Reg2:=Reg3; Reg3:=Reg4; Reg4:=0; Key:=#0;
   KeyOK:=true; UpdDsp; NewReg:=true; SavReg:=true;
   end;

========================
It is all simple stuff. It has the capacity to generate numbers that are too large, but my exception trap catches them. It doesn't crash by those operations. Everything looks good. It only crashes when closing the application, and it has nothing to do with large numbers, though debugging is saying that it does ... with 2+2=4, and they were entered by the keyboard. Both buttons and keyboard are using the UpdDsp (update display) function.

If you think that I must, I can upload the entire project. It's just a calculator.
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Windows 10 and FormClose
« Reply #32 on: July 03, 2019, 07:22:44 pm »
Quote
Code snippets like these are not very helpful
I have posted the code because ASerge asked me to.
I am asking for it, too. Having no compilable code often leads to endless discussions because everybody does and understands things a bit different.
+1. With this piece of code the error does not reproduce.

metis

  • Sr. Member
  • ****
  • Posts: 300
Re: Windows 10 and FormClose
« Reply #33 on: July 04, 2019, 01:01:06 pm »
@rick2691

Quote
I tried the SetExceptionMask...It did not help
You must put it at the very Beginning of the Program - Did You do so ?

Quote
The program is a calculator + I do not believe that these things are due to my coding
I found a Calculator (normal-scientific-graphic) in my DelphiCode-Collection (see attached 'Calculator.7z').
Though I never tried it out, maybe it's helpful for Your App, maybe this Code also behaves strange on Win10.

Quote
It only crashes when closing the application
Did You try out the HeapTrc-Unit (-gh Switch) to check Memory-Allocation/Deallocation ?
Life could be so easy, if there weren't those f*** Details.
My FFmpeg4Lazarus = FFPlay4Laz + FFGrab4Laz + FFInfo4Laz

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Windows 10 and FormClose
« Reply #34 on: July 04, 2019, 05:25:32 pm »
It is really difficult to send an attachment. It has posted as an another thread with the same title.
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

metis

  • Sr. Member
  • ****
  • Posts: 300
Re: Windows 10 and FormClose
« Reply #35 on: July 04, 2019, 06:42:19 pm »
@rick2691

Quote
It has posted as an another thread with the same title
Hope, You agree, that I copied Your Post from the other Thread over here to keep continuity:
(So, my previous Post served as a placeholder)   :)

***************************************************************************************

@metis

HeapTrc-Unit (-gh Switch):

memory blocks allocated and freed equal each other

unfreed memory blocks equal zero

true heap = 229376
true free heap = 229248

true free heap should = 229376

====

SetExceptionMask:

I did not put it at the start.
I put it at the start of FormCreate.

Calculator.7z:

He uses a different approach but it looks essentially the same by my review.

=================

attached: Laz-CmdRPN - Copy.zip

This is the entire project. It is 32bit. It is a scientific RPN (reverse-polish-notation) calculator.
Entry example: 32 enter 20 plus ... the result is 52.

***************************************************************************************
« Last Edit: July 11, 2019, 01:56:35 pm by metis »
Life could be so easy, if there weren't those f*** Details.
My FFmpeg4Lazarus = FFPlay4Laz + FFGrab4Laz + FFInfo4Laz

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Windows 10 and FormClose
« Reply #36 on: July 04, 2019, 07:02:10 pm »
I compiled and ran the program posted in the other thread (https://forum.lazarus.freepascal.org/index.php/topic,45983.msg325974) - no issue, no crash when I close the program. What exactly do I have to do to reproduce the error?

I do:
- click '5', '5'   (--> displays '55')
- click 'ENTER'  (--> displays '55.00')
- click '2', '2'   (--> displays '22')
- click '+' (--> displays '77.00')
- click close-x in the title bar of the form
--> program closes without an error (there is a heaptrc message, but this is expected).

OS: Win 10, 64 bit (v1903)
Laz trunk / fpc 3.0.4  / 32 bit, or Laz 1.6.4 / fpc 3.0.2 / 32 bit
« Last Edit: July 04, 2019, 07:17:32 pm by wp »

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Windows 10 and FormClose
« Reply #37 on: July 04, 2019, 08:45:37 pm »
I always type enough digits to allow for a / * - and +, but I do it all from the keyboard. If it isn't showing, I do it again and also do a Sqr, SqRt, Sin and ASine. Always closing by the CloseButton at the top of the form.
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Windows 10 and FormClose
« Reply #38 on: July 04, 2019, 11:43:12 pm »
@wp

You also need to launch from an exe. If you do it by run, it is protected.
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Windows 10 and FormClose
« Reply #39 on: July 05, 2019, 01:46:35 pm »
Still cannot reproduce...

When scanning your code I saw these things which are unusual. I think, however, that they do not cause your issues:
  • Unit "win32proc" normally is not needed in an LCL program. I can remove it without any issues
  • FormClose: Assigning caFree to CloseAction seems to be redundant because the main form of an application is automatically destroyed when it is closed. I can remove this line without negative effect.
  • Like many others, you use the form's name in the code of the class. This is badly wrong because it makes your program fail when you'll once decide to rename the form instance or to require another differently named instance of that class. Just remove "AppForm." in all TAppForm methods. If there should be a naming conflict with other identifiers use "self." instead.

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Windows 10 and FormClose
« Reply #40 on: July 06, 2019, 07:10:40 pm »
I took out Win32Proc. It had a minor improvement. This is with a Win8 compatibility, and the highest security level that I could invoke. The mathematics did not crash it, but it I left it running and  ran other programs, upon returning and closing it, it crashed as before.

I compiled it again as a WinXP compatibility, by placing it in another folder, it returned to the mathematics crash at closing.

So I presume it is securities and compatibility that is creating the problem.

Any ideas on how to overcome this?
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Windows 10 and FormClose
« Reply #41 on: July 06, 2019, 07:26:21 pm »
Technically I wouldn't call it a crash. It does everything that is supposed to be done. All the registries are are saved. It is more like an interrupt that hangs on after the destroy sequence.

Its message is ... An unhandled exception occurred at $0040F338: $0040F338
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Windows 10 and FormClose
« Reply #42 on: July 07, 2019, 12:41:42 am »
I sometimes get an error when I quickly perform several actions and exit the program (by Alt+F4 in Windows 10 and in Windows 7). Error occurs in the releasing a dynamic string in the units finalization. Occurs only for a 32-bit application. 64-bit works without failures.
Try to get rid of global string variables.

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Windows 10 and FormClose
« Reply #43 on: July 07, 2019, 01:27:35 am »
32-bit Windows programs needs SEH exception handling. FPC 3.0.4 doesn't support this. FPC 3.2.x (fixes) and trunk have support for this but you need to compile them with -dTEST_WIN32_SEH option.

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Windows 10 and FormClose
« Reply #44 on: July 07, 2019, 06:11:37 pm »
@ASerge

I have a dozen other programs that are Win32, very much larger, more complex, many more globals, and they do not have this exception issue.

Why is that?
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

 

TinyPortal © 2005-2018