Jump to content
Paul Dardeau

Guidance on FreeAndNil for Delphi noob

Recommended Posts

4 hours ago, Darian Miller said:

One step further... I worked with a Delphi developer who rarely used Free, and tried to only use Destroy.  Everyone seems to have an opinion on Free vs FreeAndNil but he was the only one that ever went further and said Destroy vs Free.  Some of the same arguments could be had for using Destroy vs Free.  (But I still use .Free)

This one is easy to debunk. Exceptions raised in a constructor then lead to exceptions in the destructor. This is the entire reason why Free exists. 

  • Like 2

Share this post


Link to post
20 hours ago, Brandon Staggs said:

Interesting that Bauer and Primoz take opposite sides.

I can't see that they do.

 

Here's the TLDR as I read it:

Allen is saying that you shouldn't (mindless or not) use FreeAndNil to solve logic/design bugs and AFAICT nobody is disagreeing with that.

Primoz and David are saying that they are using it to find stale reference bugs.

 

Allen's suggestion to use a debug memory manager instead isn't valid because you can't do that on a production system, as Primoz also points out.

Edited by Anders Melander
  • Like 1

Share this post


Link to post
Posted (edited)
On 2/21/2025 at 5:45 PM, Paul Dardeau said:

Embarcadero hosted a youtube livestream the other day with MVPs.

Where the hell is the link to that video. I wanted for over an year now to see that video. It was never released!

 

Google does not know such video:

https://www.google.com/search?client=firefox-b-d&q=free+and+nil+the+great+debate

Edited by GabrielMoraru

Share this post


Link to post
Posted (edited)

Nothing ..... wrong replay

 

Edited by DelphiUdIT

Share this post


Link to post
Just now, GabrielMoraru said:

Where the hell is the link to that video. I wanted for over an year now to see that video. It was never released!

 

 

  • Like 1

Share this post


Link to post
Posted (edited)

My "3" cents:

1. FreeAndNil will definitively help you if you have stale pointers.

2. FreeAndNil costs only few more CPU cycles compared to Free().

3. Some people with big mouth 🙂 suggested that FreeAndNil could be bad (somehow magically it will break your code), BUT nobody was able to post a piece of compilable code to prove it 🙂 . Until they do, they remain "big mouth" 🙂 . [Yes, this is a challenge]

 

But, yes, don't design your code architecture around it.

 

_

 

That being said, since it is so cheap to use it and might bring benefits, I do use it.

 

PS: There were cases where it really helped. 

For example I had problems with a "use after free" in the ccrExif library. They were using an object after free. Replacing all .Free with FreeAndNil "magically" solved the "mystery". 

Edited by GabrielMoraru

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×