Do I have to allocate and free records when using TList<T> in Delphi?
Posted
by afrazier
on Stack Overflow
See other posts from Stack Overflow
or by afrazier
Published on 2010-04-24T01:04:41Z
Indexed on
2010/04/24
1:13 UTC
Read the original article
Hit count: 306
The question more or less says it all. Given the following record structure:
type
TPerson = record
Name: string;
Age: Integer;
end;
PPerson = ^TPerson;
TPersonList = TList<TPerson>;
Is the following code valid?
procedure ReadPeople(DataSet: TDataSet; PersonList: TPersonList);
begin
PersonList.Count := DataSet.RecordCount;
if DataSet.RecordCount = 0 then
Exit;
DataSet.First;
while not DataSet.Eof do begin
PersonList[DataSet.RecNo].Name := DataSet.FieldByName('Name').AsString;
PersonList[DataSet.RecNo].Age := DataSet.FieldByName('Age').AsInteger;
DataSet.Next;
end;
end;
Do I have to use GetMem
/FreeMem
to allocate and free records an instance of TPersonList
, or am I free to directly access the TPersonList
entries directly? My gut says that the code should be valid, though I'm not sure if there's any wrinkles related to record initialization or finalization.
© Stack Overflow or respective owner