2

I have a doubt that whether FreeAndNil is the ideal approach to dispose an object . I have come across codes where something like

StringList1.Free; 
StringList1 := Nil;

is used. Is the above line an overkill , or is it a better way to do things ? What is the difference between the two approaches?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
CyprUS
  • 4,159
  • 9
  • 48
  • 93
  • possible duplicate of [Which is preferable: Free or FreeAndNil?](http://stackoverflow.com/questions/3159376/which-is-preferable-free-or-freeandnil) – Lieven Keersmaekers May 11 '12 at 06:28
  • 1
    i see that there is a seperate SO discussion already on this topic. Thus , i think it is better if it is closed. – CyprUS May 11 '12 at 06:40
  • Well, that depends on what this question is. I read it as the difference between FreeAndNil and the two lines of code in this question. If that reading is correct then this is not a dupe. What is your question? – David Heffernan May 11 '12 at 07:06
  • @DavidHeffernan : Yes , you read it right. Thanks for the help. I understood the diff. – CyprUS May 11 '12 at 09:37

2 Answers2

9

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.

Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128
  • Note that FreeAndNil is actually NilThenFree on the inside, order might be important in some cases. – Kromster Jul 10 '14 at 19:09
5

The code in your question is probably written by someone that doesn't know about FreeAndNil. Or perhaps it was originally written before FreeAndNil was added to the RTL. If you want to nil the reference then you may as well use FreeAndNil. Writing it out longhand doesn't help.

The only real difference is that FreeAndNil will set the reference to Nil even if Free raises. But your destructors should never raise anyway so this is not that big a deal in my view.

There is a common trap with FreeAndNil. Because it takes an untyped parameter, you can pass it anything. For example you can pass an interface, a record etc. If you make this mistake then you usually end up with strange runtime errors.

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • +1 for trying to avoid the unavoidable general discussion about `Free` vs `FreeAndNil` :) – jpfollenius May 11 '12 at 06:32
  • i didnt understand what you meant by "If you want to nil the reference then you may as well use FreeAndNil". Also , if you have any good reference , give the link , i would like to know in depth – CyprUS May 11 '12 at 06:38
  • 1
    FreeAndNil is trivially simple. The code says it all. My point is that if you want to free and object, and nil the reference, then call FreeAndNil rather than doing it in two separate steps. I can't really say it any clearer. – David Heffernan May 11 '12 at 06:50
  • @naXa: I've rolled back your edit. Code formatting is to be used only on **code**, and the mere mention of a function name in text does not make it code. – Ken White Jul 10 '14 at 18:23
  • 2
    @naXa, KenWhite: I agree with naXa, highlighting function names in otherwise monotone text is only helpful. – Kromster Jul 10 '14 at 19:08