Dalija Prasnikar Profile picture
Jun 29 12 tweets 3 min read
FreeAndNil() and Assigned() debate is coming up soon blogs.embarcadero.com/freeandnil-del…

This may seem like simple topic, but in order to discuss when and whether one should use FreeAndNil() and Assigned(), we need to understand related, more complex processes happening in our code. Image
In Delphi object references are generally not automatically initialized to zero (nil).
Only class fields, object instance fields and global variables are automatically initialized.
Local object references and record fields are not initialized.
Working with uninitialized object references is undefined behavior.
Therefore, calling Assigned() or otherwise checking for nil on uninitialized reference is also undefined behavior. In other words, result will be random, based on accidental content of the references.
Construction of any object instance can raise exceptions.

The destruction process must never raise exceptions.

Unhandled exceptions (ones not caught and handled by try..except block) in destructors or BeforeDestruction will cause unrecoverable memory leaks.
If an exception (unhandled) is raised inside constructor chain or AfterConstruction, it will trigger automatic cleanup. Destructors and other related methods must be capable of cleaning up partially constructed instances.
If the exception is raised inside constructor, cleanup will call only destructor chain.
If the exception is raised inside AfterConstruction, cleanup will start by calling BeforeDestruction and then destructor chain.
The automatic cleanup process ensures that exceptions raised during object construction don't have negative impact on the application stability and memory usage.
But, that is valid only if destruction does not raise unhandled exceptions.
Calling Destroy, which is dynamically dispatched (virtual) method resolved at runtime, on nil reference will raise an exception (access violation).
On the other hand, Free method can be safely called on nil reference.
It is statically dispatched (resolved at compile time) and
it also checks whether instance (Self) is nil before calling Destroy.

In other words, calling Free on nil reference will not raise an exception.
Because Free can be safely called on nil reference, it must be used in destructors (or other methods called during destruction process) instead of Destroy.

You also need to check for nil or call Assigned if you need to call other methods besides Free on object reference there.
Because Free can be safely called on nil reference checking for nil before calling Free is useless.

if Assigned(Obj) then Obj.Free; ❌

https://t.co/t1rXy9SL3S; ✅
Of course, this is not all there is to be said on the topic and is just scratching the surface...

If you want to know more, register for the webinar: register.gotowebinar.com/register/88518…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Dalija Prasnikar

Dalija Prasnikar Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(