Delphi,如何在TList中自由记录

时间:2015-08-24 19:02:09

标签: delphi

我想知道释放充满记录的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);

感谢您的帮助,感谢您!

1 个答案:

答案 0 :(得分:4)

您已使用构造函数定义了记录。调用记录构造函数不会像堆类构造函数那样在堆上分配内存。您的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;