gpt4 book ai didi

Delphi,如何在TList中释放记录

转载 作者:行者123 更新时间:2023-12-02 07:26:10 29 4
gpt4 key购买 nike

我想知道释放充满记录的 TList 的最佳方法。

我有以下记录:

type
TPkBill = record
PkBill: integer;
Constructor Create(c_PkBill: integer);

constructor TPkBill.Create(c_PkBill: integer);
begin
PkBill := c_PkBill;
end;

我创建列表并用记录填充它:

procedure TfrmProject.lvBillDblClick(Sender: TObject);
var
i, iCount: integer;
item: TListItem;
oPkBill: TPkBill;
lstPkBill: Tlist;
begin
iCount := 0;
lstPkBill:= TList.Create;

//if an item is selected in lv
if (lvBill.ItemIndex = -1) then begin exit; end
else
begin
//Loop through all items and get selected item
for i := 0 to lvBill.Items.Count - 1 do
begin
item := lvBill.Items.Item[i];
if(item.Selected = true)then
begin
//create new item
oPkBill := TPkBill.Create(StrToInt(lvBill.Items[i].Caption));
//add it to a list
lstPkBill.Add(TObject(oPkBill));
//add up
iCount := iCount +1;
end;
end;
//Now we have a list ok pkBill

if(iCount > 1)then //other stuff I do
end

我希望能够释放 TList 并释放记录。

这是我已经尝试过的:

for i := 0 to lstPkBill.Count - 1 do
begin
//TObject(TPkBill(lstPkBill[i])).Free; //Acces violation at adress..
//FreeMem(TPkBill(lstPkBill[i])); //Incompatible types
//FreeMem(TObject(lstPkBill[i])); //Incompatible types
end;
lstPkBill.Clear;
FreeAndNil(lstPkBill);

感谢您的帮助,不胜感激!

最佳答案

您已使用构造函数定义了一条记录。调用记录构造函数不会像类构造函数那样在堆上分配内存。您的 oPkBill 变量存在于堆栈中。调用 oPkBill := TPkBill.Create(...) 只是填充该变量的成员。然后,您将整个变量(仅包含一个 Integer 成员)类型转换为 TObject 指针。您实际上并未在堆上为列表项分配任何内存,因此无需释放它们。

我怀疑你实际上想做的事情更像是这样的:

type
PPkBill = ^TPkBill;
TPkBill = record
PkBill: integer;
Constructor Create(c_PkBill: integer);
end;

constructor TPkBill.Create(c_PkBill: integer);
begin
PkBill := c_PkBill;
end;

procedure TfrmProject.lvBillDblClick(Sender: TObject);
var
i: Integer;
item: TListItem;
oPkBill: PPkBill;
lstPkBill: TList;
begin
if lvBill.ItemIndex = -1 then Exit;

//an item is selected in lv

lstPkBill := TList.Create;
try
//Loop through all items and get selected items
for i := 0 to lvBill.Items.Count - 1 do
begin
item := lvBill.Items.Item[i];
if item.Selected then
begin
//create new item
New(oPkBill);
try
oPkBill^ := TPkBill.Create(StrToInt(lvBill.Items[i].Caption));
//add it to a list
lstPkBill.Add(oPkBill);
except
Dispose(oPkBill);
raise;
end;
end;
end;

//Now we have a list ok pkBill
if (lstPkBill.Count > 1) then
begin
//other stuff I do
end;
finally
for i := 0 to lstPkBill.Count - 1 do
Dispose(PPkBill(lstPkBill[i]));
lstPkBill.Free;
end;
end;

关于Delphi,如何在TList中释放记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32189776/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com