Recent

Author Topic: Access Violation on clicking buttons and deleting text  (Read 5727 times)

Paul-SFL

  • New Member
  • *
  • Posts: 22
Access Violation on clicking buttons and deleting text
« on: September 25, 2011, 01:42:24 pm »
Hi all :) ,

I finally wrote the calculator mode and now trying to connect it with GUI. But somewhy I get access violation errors on testing the calculator:
  • when type a number and then delete it by pressing BackSpace (error when I delete the latest char);
  • when clicking "+", "-" or "C" after typing the number
  • when clicking "="

Unit references seems to be correct.. I have no idea on why  does it happen :( . The calculator "engine", gncalc, has been tested - works 100%-correctly, so I believe the problem is in unit2.

I'll appreciate very much if someone skims over the source and gives me some advice.

I'm using Lazarus 0.9.30 + FRC 2.4.2.
« Last Edit: September 25, 2011, 07:33:20 pm by Paul-SFL »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Access Violation on clicking buttons and deleting text
« Reply #1 on: September 25, 2011, 03:16:55 pm »
There are more bugs. It gives me SIGSEGV on closing the app.

Anyway, you should add there something like:
Code: [Select]
procedure Tcalculator_mode.fieldChange(Sender: TObject);
     var s:string;
begin
  field.Readonly:=false;
  if length(field.text)=0 then exit;  //ADD THIS LINE
  if ((field.text[1]='-') and (length(field.text)>254)) or (length(field.text)>253)       then         
because you are trying to read field.text[1] even if field.text is empty.

EDIT:
You shouldn't have Application.Terminate on closing of main form. When you close main form then app is terminated automaticly.
Code: [Select]
procedure Tcalculator_mode.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
//application.Terminate;
end;   

EDIT2:
Bug: You delete something and then you assign it ! :-)
Code: [Select]
procedure Tcalculator_mode.plusClick(Sender: TObject);
begin
  gncalc.plus:=true;
  field.text:='';
  gncalc.Operand_0:=field.text;
end;     

must be :
Code: [Select]
procedure Tcalculator_mode.plusClick(Sender: TObject);
begin
  gncalc.plus:=true;
  gncalc.Operand_0:=field.text;
  field.text:='';
end;     

And TMemo loses focus after pressing "C".
« Last Edit: September 25, 2011, 04:36:48 pm by Blaazen »
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Paul-SFL

  • New Member
  • *
  • Posts: 22
Re: Access Violation on clicking buttons and deleting text
« Reply #2 on: September 25, 2011, 07:33:02 pm »
Thank you a lot, Blaazen! It works!
Code: [Select]
  if length(field.text)=0 then exit;  //ADD THIS LINE   
Done.
You shouldn't have Application.Terminate on closing of main form. When you close main form then app is terminated automaticly.
Thanks for the advice. Actually, I set calculator form to be main temporarily :) .
EDIT2:
Bug: You delete something and then you assign it ! :-)
Code: [Select]
procedure Tcalculator_mode.plusClick(Sender: TObject);
begin
  gncalc.plus:=true;
  field.text:='';
  gncalc.Operand_0:=field.text;
end;     

must be :
Code: [Select]
procedure Tcalculator_mode.plusClick(Sender: TObject);
begin
  gncalc.plus:=true;
  gncalc.Operand_0:=field.text;
  field.text:='';
end;     
Fixed. I also added to every button click procedure a boolean which shows whether the first operand is entered, and if it's not - the procedures are exited without doing anything.
And TMemo loses focus after pressing "C".

Fixed too. BTW it's an interesting thing: TMemo loses focus on pressing "=" or "C" and stays focused on pressing "+" or "-" - why does it happen?

EDIT:
So, finally I've got it 100% working :) .
Blaazen, thank you once more - you saved a lot of my time, I think.
« Last Edit: September 25, 2011, 07:54:55 pm by Paul-SFL »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Access Violation on clicking buttons and deleting text
« Reply #3 on: September 25, 2011, 07:55:23 pm »
Quote
BTW it's an interesting thing: TMemo loses focus on pressing "=" or "C" and stays focused on pressing "+" or "-" - why does it happen?
Because "+" and "-" are TSpeedButton, which cannot be focused. "=" and "C" are TButton.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Paul-SFL

  • New Member
  • *
  • Posts: 22
Re: Access Violation on clicking buttons and deleting text
« Reply #4 on: September 25, 2011, 08:09:11 pm »
Thanks, got it :) .

 

TinyPortal © 2005-2018