gpt4 book ai didi

delphi - 记录类型的 TList 泄漏内存

转载 作者:行者123 更新时间:2023-12-03 15:52:04 27 4
gpt4 key购买 nike

代码大致如下所示,它会为创建的每个 TMyRecord 中的每个字符串实例泄漏内存。我想我必须访问每条记录并以某种方式释放它——这可以在不清除每个单独字符串的情况下完成吗?

function TMyForm.RecordFromThing(thing): TMyRecord;
begin
result.StringVal1 = thing.SomeProperty;
result.StringVal2 = thing.SomeOtherProperty;
end;

function TMyForm.RecordsFromItems: TList<TMyRecord>;
begin
result := TList<TMyRecord>.Create;
for thing in things do
begin
result.Add(RecordFromThing(thing));
end;
end;

procedure TMyForm.Button1Click(Sender: TObject);
var Items: TList<TMyRecord;
begin

Items := RecordsFromItems;
try
//Stuff
finally
// What goes here to free those records?
Items.Clear;
Items.Free;
end

end;

根据 David 的要求,这是从我的应用程序中剪切并粘贴的实际代码。如果我运行 Button1Click,则 FastMM 会报告字符串内存泄漏。

  type TPremiumPaymentInstruction = record
SupplierID: string;
FirstName: string;
LastName: string;
PolicyNo: string;
CarrierName: string;
PayAmount: string;
DueDate: string;
PayMethod: string;
Comments: string;
Payee: string;
Address: string;
Address2: string;
City: string;
State: string;
Zip: string;
InRe: string;
BankName: string;
BankABA: string;
AccountName: string;
AccountNo: string;
CreditTo: string;
end;

function TPremiumPaymentManager.RecordFromItem(Item: TListItem): TPremiumPaymentInstruction;
var PolicyID: integer;
Instructions: TDataSet;

function FormatNote(PolicyNo, First, Last: string): string;
begin
result := 'Policy# ' + PolicyNo + '; Insured Name: ' + Last + ' ' + First;
end;

begin

FillChar(result, SizeOf(result), 0);

result.SupplierID := Item.Caption;
result.FirstName := Item.SubItems[INSURED_FIRST_NAME_COLUMN];
result.LastName := Item.SubItems[INSURED_LAST_NAME_COLUMN];
result.PolicyNo := Item.SubItems[POLICY_NUMBER_COLUMN];
result.CarrierName := Item.SubItems[CARRIER_NAME_COLUMN];
result.PayAmount := Item.SubItems[ACTUAL_COLUMN];
result.DueDate := Item.SubItems[DATE_DUE_COLUMN];
result.PayMethod := Item.SubItems[PAYMENT_METHOD_COLUMN];

PolicyID := GetSingleValue('SELECT PolicyID FROM PremiumsDue WHERE PremiumID = :PremiumID;', [(Item as TAdvListItem).KeyValue], 0);
Instructions := GetDS('SELECT I.* FROM Policies P INNER JOIN CarrierPaymentInstructions I ON P.CarrierID = I.CarrierID AND P.PaymentInstruction = I.InstructionDescription WHERE P.PolicyID = ?;', [PolicyID]);

try

Instructions.Open;
Instructions.First;

if result.PayMethod = 'Check' then
begin
result.Comments := Item.SubItems[PAYMENT_NOTE_COLUMN];
result.Payee := Instructions.FieldByName('PayTo').AsString;
result.Address := Instructions.FieldByName('Address1').AsString;
result.Address2 := Instructions.FieldByName('Address2').AsString;
result.City := Instructions.FieldByName('City').AsString;
result.State := Instructions.FieldByName('State').AsString;
result.Zip := Instructions.FieldByName('ZipCode').AsString;
result.InRe := FormatNote(result.PolicyNo, result.FirstName, result.LastName);
end
else
begin
result.BankName := Instructions.FieldByName('PayTo').AsString;
result.BankABA := Instructions.FieldByName('ABANumber').AsString;
result.Address2 := Instructions.FieldByName('Address2').AsString;
result.AccountName := Instructions.FieldByName('AccountName').AsString;
result.AccountNo := Instructions.FieldByName('AccountNumber').AsString;
result.CreditTo := FormatNote(result.PolicyNo, result.FirstName, result.LastName);
end;

finally

Instructions.Free;

end;

end;

function TPremiumPaymentManager.RecordsFromItems: TList<TPremiumPaymentInstruction>;
var item: TListItem;
begin

result := TList<TPremiumPaymentInstruction>.Create;

for item in lvPremiums.Items do
begin
if (not (Item as TAdvListItem).Strikeout) and (Item.SubItems[ACTUAL_COLUMN] <> '') then
result.Add( RecordFromItem(item) );
end;

end;

procedure TPremiumPaymentManager.Button1Click(Sender: TObject);
var Items: TList<TPremiumPaymentInstruction>;
begin

Items := RecordsFromItems;
Items.Clear;
Items.Free;

end;

最佳答案

字符串是托管类型。编译器负责它们的分配和释放。编译器编写维护字符串数据引用计数的代码。当引用计数变为零时,内存将被释放。

因此,您不需要释放字符串或执行任何显式管理。任何这样做的尝试都可能与编译器执行的操作发生冲突。例如,如果您对字符串变量执行原始内存访问,则通过调用FillChar看到。 ,那么引用计数将被绕过。

您问题中的代码除了不完整和部分无法编译之外,本质上是好的。当您完成 TList<TMyRecord> 后只需释放它即可。编译器将生成代码来释放所有引用。甚至不需要清除它。

List := TList<TMyRecord>.Create;
try
List.Add(...);
// etc.
finally
List.Free;
end;

这就是您所需要的。

由于您问题中的代码本质上与此相同,因此我得出结论,泄漏的原因在其他地方。

<小时/>

你知道什么,你的编辑包含对 FillChar 的调用。将其替换为

Result := Default(TPremiumPaymentInstruction);

您总是需要初始化返回值。甚至返回经常被初始化的托管类型的值。但当以您的方式在 for 循环内进行调用时则不然。去搞清楚。无论如何,总是初始化返回值。和Default(T)是你的 friend 。

关于delphi - 记录类型的 TList 泄漏内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28798842/

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