如何一次正确释放Delphi中包含各种类型的记录? [英] How to properly free records that contain various types in Delphi at once?
问题描述
type
TSomeRecord = Record
field1: integer;
field2: string;
field3: boolean;
End;
var
SomeRecord: TSomeRecord;
SomeRecAr: array of TSomeRecord;
这是我所拥有的最基本的例子,因为我想重用 SomeRecord
(某些字段保持为空,没有释放所有内容,当我重用 SomeRecord
,这显然是不受欢迎的)我正在寻找一种方法来一次释放所有字段.我开始使用 string[255]
并使用 ZeroMemory()
,这很好,直到它开始泄漏内存,那是因为我切换到 string代码>.我仍然缺乏了解原因的知识,但这似乎与它的动态有关.我也在使用动态数组,所以我假设在任何动态上尝试
ZeroMemory()
都会导致泄漏.浪费了一天弄清楚这一点.我想我通过在 ZeroMemory()
之前在 SomeRecord
或 SomeRecAr
上使用 Finalize()
解决了这个问题,但我我不确定这是正确的方法还是只是我愚蠢.
This is the most basic example of what I have and since I want to reuse SomeRecord
(with certain fields remaining empty, without freeing everything some fields would be carried over when I'm reusing SomeRecord
, which is obviously undesired) I am looking for a way to free all of the fields at once. I've started out with string[255]
and used ZeroMemory()
, which was fine until it started leaking memory, that was because I switched to string
. I still lack the knowledge to get why, but it appears to be related to it being dynamic. I am using dynamic arrays as well, so I assume that trying ZeroMemory()
on anything dynamic would result in leaks. One day wasted figuring that out. I think I solved this by using Finalize()
on SomeRecord
or SomeRecAr
before ZeroMemory()
, but I'm not sure if this is the proper approach or just me being stupid.
所以问题是:如何一次释放所有内容?是否存在一些我不知道的单一程序?
So the question is: how to free everything at once? does some single procedure exist at all for this that I'm not aware of?
换个说法,或者我愿意接受建议如何以不同的方式实现这些记录,所以我不需要在释放东西时进行复杂的尝试.我研究过使用 New()
创建记录,然后删除它 Dispose()
,但我不知道调用后的变量是什么意思Dispose()
是未定义的,而不是 nil.另外,我不知道某种类型的变量(SomeRecord: TSomeRecord
)与指向某个类型的变量(SomeRecord: ^TSomeRecord
)有什么区别.我目前正在研究上述问题,除非有人能快速解释,否则可能需要一些时间.
On a different note, alternatively I would be open to suggestions how to implement these records differently to begin with, so I don't need to make complicated attempts at freeing stuff. I've looked into creating records with New()
and then getting rid of it Dispose()
, but I have no idea what it means when a variable after a call to Dispose()
is undefined, instead of nil. In addition, I don't know what's the difference between a variable of a certain type (SomeRecord: TSomeRecord
) versus a variable pointing to a type (SomeRecord: ^TSomeRecord
). I'm looking into the above issues at the moment, unless someone can explain it quickly, it might take some time.
推荐答案
假设您有一个支持在记录上实现方法的 Delphi 版本,您可以像这样清除记录:
Assuming you have a Delphi version that supports implementing methods on a record, you could clear a record like this:
type
TSomeRecord = record
field1: integer;
field2: string;
field3: boolean;
procedure Clear;
end;
procedure TSomeRecord.Clear;
begin
Self := Default(TSomeRecord);
end;
如果你的编译器不支持 Default
那么你可以像这样简单地做同样的事情:
If your compiler doesn't support Default
then you can do the same quite simply like this:
procedure TSomeRecord.Clear;
const
Default: TSomeRecord=();
begin
Self := Default;
end;
<小时>
您可能更愿意避免在方法中改变值类型.在这种情况下,创建一个返回空记录值的函数,并将其与赋值运算符一起使用:
You might prefer to avoid mutating a value type in a method. In which case create a function that returns an empty record value, and use it with the assignment operator:
type
TSomeRecord = record
// fields go here
class function Empty: TSomeRecord; static;
end;
class function TSomeRecord.Empty: TSomeRecord;
begin
Result := Default(TSomeRecord);
end;
....
Value := TSomeRecord.Empty;
<小时>
顺便说一句,我找不到 Default(TypeIdentifier)
的任何文档参考.有谁知道哪里可以找到吗?
As an aside, I cannot find any documentation reference for Default(TypeIdentifier)
. Does anyone know where it can be found?
至于您问题的第二部分,我认为没有理由不继续使用记录并使用动态数组分配它们.尝试自己管理生命周期更容易出错.
As for the second part of your question, I see no reason not to continue using records, and allocating them using dynamic arrays. Attempting to manage the lifetime yourself is much more error prone.
这篇关于如何一次正确释放Delphi中包含各种类型的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!