English 中文(简体)
德尔菲7中的“自由安道尔”的替代办法是什么?
原标题:What is the alternative to FreeAndNil in Delphi 7?

我怀疑自由安道尔是否是处置物体的理想办法。 我走过了各种法典,像一些这样的东西。

StringList1.Free; 
StringList1 := Nil;

使用。 以上一线是高技能的,还是更好做事? 这两种办法之间的区别是什么?

最佳回答

Strictly speaking, to dispose of an object you only need to call Destroy. However, if the reference is nil, ie. has not been assigned a pointer to a valid instance of an object, this would cause an access violation, so it s recommended to call Free instead which checks first if the passed reference is nil and does nothing in that case. FreeAndNil first assigns the passed reference to a temporary local variable, then assigns nil to the passed reference, then calls Free to dispose of the instance. The only difference between FreeAndNil and your sequence obj.Free; obj := nil; is that in the first case the passed reference will be nil even if the destructor raises an exception.

问题回答

贵问题中的守则可能由那些对没有了解的人撰写。 或者说,最初写在FreeAndNil之前,该文本已添加到RTL中。 如果你想要打字,你也可以使用<代码>FreeAndNil。 用长篇字书写,无助。

唯一的实际差异是FreeAndNil将提及。 Nil 即使有Free的提出。 但是,你们的信徒永远不应有任何进展,因此,我认为,这不是一件大事。

有一种常见的陷阱是FreeAndNil。 由于它带有不定的参数,你可以穿透任何东西。 例如,你可以通过接口、记录等。 如果你犯了这一错误,你通常会最后出现奇怪的错误。

I m not going to get started on whether or not FreeAndNil is an appropriate design choice. That topic has been covered in some depth elsewhere.





相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签