Parcourir la source

Cosmetic; slight hardening of memory management

Reinier Olislagers il y a 11 ans
Parent
commit
ffce83b505
1 fichiers modifiés avec 29 ajouts et 26 suppressions
  1. 29 26
      querywindow.pas

+ 29 - 26
querywindow.pas

@@ -215,6 +215,8 @@ type
       var meResult: TMemo; AdditionalTitle: string = ''): TTabSheet;
     // Runs SQL script; returns result
     function ExecuteScript(Script: string): Boolean;
+    // Adds control/object to list of programmatically created items.
+    // Their memory will be freed in code when no longer used
     procedure AddResultControl(ParentControl: TObject; AControl: TObject);
     procedure NewApplyButton(var Pan: TPanel; var ATab: TTabSheet);
     procedure RemoveControls;
@@ -362,23 +364,25 @@ end;
 procedure TfmQueryWindow.InsertModifiedRecord(RecordNo, TabIndex: Integer);
 var
   i: Integer;
-  Exist: Boolean;
+  Exists: Boolean;
 begin
-  Exist:= False;
+  Exists:= False;
   if TabIndex > High(fModifiedRecords) then // Insert new tab
   begin
     SetLength(fModifiedRecords, TabIndex + 1);
   end;
 
-  // check if record already inserted
+  // Check if record already inserted
   for i:= 0 to High(fModifiedRecords[TabIndex]) do
-  if fModifiedRecords[TabIndex][i] = RecordNo then
   begin
-    Exist:= True;
-    Break;
+    if fModifiedRecords[TabIndex][i] = RecordNo then
+    begin
+      Exists:= True;
+      Break;
+    end;
   end;
 
-  if not Exist then  // Insert record pointer
+  if not Exists then  // Insert record pointer
   begin
     setLength(fModifiedRecords[TabIndex], Length(fModifiedRecords[TabIndex]) + 1);
     fModifiedRecords[TabIndex][High(fModifiedRecords[TabIndex])]:= RecordNo;
@@ -389,7 +393,6 @@ begin
   begin
     EnableApplyButton;
   end;
-
 end;
 
 
@@ -406,6 +409,7 @@ var
   WhereClause: string;
   RecordSet: TSQLQuery;
   TabIndex: Integer;
+  //todo: review this and use regular FPC databound controls? autogenerated updatesql etc
   FieldsSQL: string;
 begin
   try
@@ -575,12 +579,13 @@ var
   i: Integer;
 begin
   for i:= 0 to High(fResultControls) do
-  if (fResultControls[i] is TDBGrid) and ((fResultControls[i] as TDBGrid).Tag = PageControl1.TabIndex) then
   begin
-    Result:= ((fResultControls[i] as TDBGrid).DataSource.DataSet as TSQLQuery).SQL.Text;
-    Break;
+    if (fResultControls[i] is TDBGrid) and ((fResultControls[i] as TDBGrid).Tag = PageControl1.TabIndex) then
+    begin
+      Result:= ((fResultControls[i] as TDBGrid).DataSource.DataSet as TSQLQuery).SQL.Text;
+      Break;
+    end;
   end;
-
 end;
 
 
@@ -1460,8 +1465,7 @@ begin
   end;
 end;
 
-
-{ AddResultControl: add new tab for query part result in current Query window }
+{ AddResultControl: add object to array of program-managed memory }
 
 procedure TfmQueryWindow.AddResultControl(ParentControl: TObject; AControl: TObject);
 begin
@@ -1496,16 +1500,17 @@ var
 begin
   for i:= High(fResultControls) downto Low(fResultControls) do
   begin
-    if fResultControls[i] is TSQLQuery then
+    if Assigned(fResultControls[i]) then
     begin
-      (fResultControls[i] as TSQLQuery).AfterScroll:= nil;
-      (fResultControls[i] as TSQLQuery).Close;
-      (fResultControls[i] as TSQLQuery).DataSource:= nil;
+      if fResultControls[i] is TSQLQuery then
+      begin
+        (fResultControls[i] as TSQLQuery).AfterScroll:= nil;
+        (fResultControls[i] as TSQLQuery).Close;
+        (fResultControls[i] as TSQLQuery).DataSource:= nil;
+      end;
+      FreeAndNil(fResultControls[i]);
     end;
-    fResultControls[i].Free;
-    fResultControls[i]:= nil;
   end;
-
   SetLength(fResultControls, 0);
   SetLength(fParentResultControls, 0);
 end;
@@ -1920,13 +1925,11 @@ begin
   SaveDialog1.DefaultExt:= '.htm';
   SqlQuery:= FindSqlQuery;
   if SqlQuery = nil then
-  begin
-    ShowMessage('There is no record set in result');
-  end
+    ShowMessage('There is no record set in result')
   else
   if (not SQLQuery.Active) or (SQLQuery.RecordCount = 0) then
     MessageDlg('No data', mtError, [mbOk], 0)
-  else
+  else // Valid data, let's try to save:
   if SaveDialog1.Execute then
   begin
     SQLQuery.DisableControls;
@@ -1944,6 +1947,7 @@ begin
     while not SQLQuery.EOF do
     begin
       Write(f, '<tr bgcolor="');
+      // Zebra stripes in output:
       if SQLQuery.RecNo mod 2 = 0 then
         Write(F, '#EEDDFF">')
       else
@@ -1961,7 +1965,6 @@ begin
     CloseFile(F);
     SQLQuery.EnableControls;
   end;
-
 end;