From c51ea356a1189246905d2b54d6d9127b6094d1bd Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Thu, 17 Jan 2008 23:26:19 +0000 Subject: [PATCH] Fix bug #1841478 "Access violation when add indexes" by keeping the instance of TFieldEditForm in memory after using it instead of recreating it each time the window is shown. The AV seems to be caused by FreeAndNil(f) but that's not reproducable when you start the exe within Delphi. However, the AV is gone now. Related changes: + Use modal result values on buttons instead of explicitely setting them via code + Move some safe code out of a try .. except because only THandledSQLError's are catched. --- source/fieldeditor.dfm | 3 +- source/fieldeditor.pas | 154 +++++++++++++++++++---------------------- 2 files changed, 73 insertions(+), 84 deletions(-) diff --git a/source/fieldeditor.dfm b/source/fieldeditor.dfm index 89f8973e..e4bce6ac 100644 --- a/source/fieldeditor.dfm +++ b/source/fieldeditor.dfm @@ -394,8 +394,8 @@ object FieldEditForm: TFieldEditForm Height = 25 Cancel = True Caption = 'Cancel' + ModalResult = 2 TabOrder = 2 - OnClick = ButtonCancelClick end object ButtonOK: TButton Left = 111 @@ -404,6 +404,7 @@ object FieldEditForm: TFieldEditForm Height = 25 Caption = 'OK' Default = True + ModalResult = 1 TabOrder = 1 OnClick = OKClick end diff --git a/source/fieldeditor.pas b/source/fieldeditor.pas index f8e5f377..e5d35a12 100644 --- a/source/fieldeditor.pas +++ b/source/fieldeditor.pas @@ -59,7 +59,6 @@ type procedure FormShow(Sender: TObject); procedure ComboBoxTypeChange(Sender: TObject); procedure AddUpdateField(Sender: TObject); - procedure ButtonCancelClick(Sender: TObject); procedure pcChange(Sender: TObject); procedure OKClick(Sender: TObject); procedure UpdateKeys(Sender: TObject); @@ -101,6 +100,7 @@ uses var klist : Array of TMysqlIndex; + FieldEditForm : TFieldEditForm; {$R *.DFM} @@ -110,19 +110,17 @@ var Create form } function FieldEditorWindow (AOwner : TComponent; AMode : TFieldEditorMode; AFieldName : String = '') : Boolean; -var - f : TFieldEditForm; begin - f := TFieldEditForm.Create(AOwner); - f.FMode := AMode; + if FieldEditForm = nil then + FieldEditForm := TFieldEditForm.Create(AOwner); + FieldEditForm.FMode := AMode; // Also remember original mode for restoring when switching pagecontrol tabs - f.FModeWhenCalled := AMode; - f.FFieldName := AFieldName; + FieldEditForm.FModeWhenCalled := AMode; + FieldEditForm.FFieldName := AFieldName; // Init both editors - f.InitFieldEditor (nil); - f.InitIndexEditor (nil); - Result := (f.ShowModal = mrOK); - FreeAndNil (f); + FieldEditForm.InitFieldEditor (nil); + FieldEditForm.InitIndexEditor (nil); + Result := (FieldEditForm.ShowModal = mrOK); end; @@ -387,67 +385,67 @@ begin end; Screen.Cursor := crSQLWait; + strAttributes := ''; // none of the 3 attributes binary, unsigned, zerofill + strNull := ''; + strDefault := ''; + strAutoIncrement := ''; + + if CheckBoxBinary.Checked = true then + strAttributes := strAttributes + ' BINARY'; + + if CheckBoxUnsigned.Checked = true then + strAttributes := strAttributes + ' UNSIGNED'; + + if CheckBoxZerofill.Checked = true then + strAttributes := strAttributes + ' ZEROFILL'; + + if (length(EditDefault.Text) > 0) and EditDefault.Enabled then + begin + strDefault := ' DEFAULT '; + // Don't escape certain default values + if (UpperCase(EditDefault.Text) = 'CURRENT_TIMESTAMP') + or (UpperCase(EditDefault.Text) = 'NULL') then + strDefault := strDefault + EditDefault.Text + else + strDefault := strDefault + esc(EditDefault.Text) + end; + + if CheckBoxNotNull.Enabled then + begin + if CheckBoxNotNull.Checked then + strNull := ' NOT'; + strNull := strNull + ' NULL'; + end; + + if CheckBoxAutoIncrement.Checked = True then + strAutoIncrement := ' AUTO_INCREMENT'; + + if (EditLength.text <> '') and EditLength.Enabled then + strLengthSet := '(' + EditLength.text + ') ' + else + strLengthSet := ''; + + if EditComment.Enabled and (length(EditComment.Text) > 0) then + strComment := ' COMMENT ' + esc(EditComment.Text); + + strPosition := ''; + case ComboBoxPosition.ItemIndex of + 0 : ; + 1 : strPosition := ' FIRST'; + else + strPosition := ' ' + ComboBoxPosition.Text; + end; + + fielddef := + ComboBoxType.Text + // Type + strLengthSet + // Length/Set + strAttributes + // Attribute + strDefault + // Default + strNull + // [NOT] Null + strAutoIncrement + // Auto_increment + strComment; // Comment + try - strAttributes := ''; // none of the 3 attributes binary, unsigned, zerofill - strNull := ''; - strDefault := ''; - strAutoIncrement := ''; - - if CheckBoxBinary.Checked = true then - strAttributes := strAttributes + ' BINARY'; - - if CheckBoxUnsigned.Checked = true then - strAttributes := strAttributes + ' UNSIGNED'; - - if CheckBoxZerofill.Checked = true then - strAttributes := strAttributes + ' ZEROFILL'; - - if (length(EditDefault.Text) > 0) and EditDefault.Enabled then - begin - strDefault := ' DEFAULT '; - // Don't escape certain default values - if (UpperCase(EditDefault.Text) = 'CURRENT_TIMESTAMP') - or (UpperCase(EditDefault.Text) = 'NULL') then - strDefault := strDefault + EditDefault.Text - else - strDefault := strDefault + esc(EditDefault.Text) - end; - - if CheckBoxNotNull.Enabled then - begin - if CheckBoxNotNull.Checked then - strNull := ' NOT'; - strNull := strNull + ' NULL'; - end; - - if CheckBoxAutoIncrement.Checked = True then - strAutoIncrement := ' AUTO_INCREMENT'; - - if (EditLength.text <> '') and EditLength.Enabled then - strLengthSet := '(' + EditLength.text + ') ' - else - strLengthSet := ''; - - if EditComment.Enabled and (length(EditComment.Text) > 0) then - strComment := ' COMMENT ' + esc(EditComment.Text); - - strPosition := ''; - case ComboBoxPosition.ItemIndex of - 0 : ; - 1 : strPosition := ' FIRST'; - else - strPosition := ' ' + ComboBoxPosition.Text; - end; - - fielddef := - ComboBoxType.Text + // Type - strLengthSet + // Length/Set - strAttributes + // Attribute - strDefault + // Default - strNull + // [NOT] Null - strAutoIncrement + // Auto_increment - strComment; // Comment - if (FMode = femFieldAdd) then begin cwin.ExecUpdateQuery( 'ALTER TABLE ' + mainform.mask(cwin.SelectedTable) + ' ' + // table @@ -495,7 +493,6 @@ begin end; end; cwin.ShowTableProperties(cwin.SelectedTable); - ModalResult := mrOK; except on E: THandledSQLError do begin @@ -508,16 +505,6 @@ end; -{*** - Cancel Form -} -procedure TFieldEditForm.ButtonCancelClick(Sender: TObject); -begin - ModalResult := mrCancel; -end; - - - {*** User selected another tab in the main-pagecontrol } @@ -860,7 +847,6 @@ begin Mainform.ChildWin.ExecUpdateQuery(query); // Refresh listColumns to display correct field-icons Mainform.ChildWin.MenuRefreshClick(Self); - ModalResult := mrOK; except On E : Exception do begin @@ -870,6 +856,8 @@ begin klist[i].Ready := False; for i := 0 to Length(modifiedKeys) - 1 do klist[modifiedKeys[i]].Modified := True; + // Keep form open + ModalResult := mrNone; end; end; end;