Access Violation on clicking buttons and deleting text lazarus / 9-Editor ---------------------------------------------------------------------- Paul-SFL · 2011-09-25 11:42 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 numberwhen 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. ---------------------------------------------------------------------- Blaazen · 2011-09-25 13:16 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". ---------------------------------------------------------------------- Paul-SFL · 2011-09-25 17:33 Thank you a lot, Blaazen! It works! Quote from: Blaazen on September 25, 2011, 03:16:55 pmCode: [Select]  if length(field.text)=0 then exit;  //ADD THIS LINE  Done. Quote from: Blaazen on September 25, 2011, 03:16:55 pmYou 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 . Quote from: Blaazen on September 25, 2011, 03:16:55 pmEDIT2: 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. Quote from: Blaazen on September 25, 2011, 03:16:55 pmAnd 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. ---------------------------------------------------------------------- Blaazen · 2011-09-25 17:55 QuoteBTW 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. ---------------------------------------------------------------------- Paul-SFL · 2011-09-25 18:09 Thanks, got it .