I also have this piece of code, they are all related anyway. This is a long one so please be gentle
I have an TStringList which has the TFieldControl objects attached to it.
TFieldControl= class(TObject)
fldLabel:TLabel;
fldControl: TWinControl;
end
;
begin
fL := TStringList.Create;
d := dS.Dataset;
d.GetFieldNames(fL);
// create fields
for i := 0 to fL.Count - 1 do
begin
if not Assigned(fL.Objects[i]) then
begin
fLd := d.FieldByName(fL[i]);
fLd.DisplayLabel := SetDisplayLabel(fLd.FieldName);
fL.Objects[i] := TFieldControl.Create;
TFieldControl(fL.Objects[i]).fLdLabel := TLabel.Create(nil);
TFieldControl(fL.Objects[i]).fLdLabel.Caption := fLd.DisplayLabel;
case fLd.DataType of
ftString, ftSmallint, ftInteger, ftWord,
ftfLoat, ftCurrency, ftBCD, ftDate, ftTime, ftDateTime,ftWideString,
ftLargeint,ftTimeStamp:
begin
e := TDBEdit.Create(pnlControls);
e.DataSource := dS;
e.DataField := fL[i];
e.Width := ctlLayoutEditWidth;
TFieldControl(fL.Objects[i]).fLdControl := e;
end;
ftMemo, ftFmtMemo, ftWideMemo:
begin
if not Assigned(sbControls) then
sbControls := TScrollBox.Create(pnlMemos);
m := TDBMemo.Create(pnlMemos);
m.DataSource := dS;
m.DataField := fL[i];
m.Parent := sbControls;
m.Align := alTop;
m.ScrollBars := ssAutoBoth;
m.Font.Name := 'Courier New';
TFieldControl(fL.Objects[i]).fLdControl := m;
end;
ftBoolean:
begin
c := TDBCheckBox.Create(pnlControls);
c.DataSource := dS;
c.DataField := fL[i];
c.Caption := fLd.DisplayLabel;
TFieldControl(fL.Objects[i]).fLdControl := c;
end;
end;
fL is TStringList index on the fieldnames which have the TFieldObject attached to it.
What I want to confirm is this:
1. If I set the OwnsObjects property of fL and I free fL, the Objects will be freed;
2. Because Objects do not automatically free their fields, the TFieldControl objects will remain. even if the Objects are freed, unless I free the FieldControl objects themselves.
3. If I free the TFieldControl objects, the controls hold will still occupy memory, because TFieldControl is not a record, and fldLabel and fldControl are allocated on the heap. If they were assigned to other objects eg. SomeFieldControl := TFieldControl, SomeFieldControl will still be pointing to the controls even if the TFieldControl was freed.
4. If I set the TLabel and TControl properties of the TFieldControl to nil, without freeing the TLabel and TControl before, SomeFieldControl would be unaffected.
5. If TFieldControl is created via a constructor and destructor freeing TFieldControl will not free fldLabel and fldControl if the destructor is not called or does not explicitly Free fldLabel and fldControl
6. if pnlControls and pnlMemo are freed all the controls on them will also be freed.
Pont 3 looks questionable, ie freeing TFieldControl will probably nil the memory pointers allocated to fldLabel and fldControl, making SomeFieldControl invalid.
If I did SomeFieldControl.Assign(FieldControl), will SomeFieldControl still be valid if FieldControl was freed without freeing fldLabel and fldControl because SomeField control uses its own storage for its pointers