There are a lot of articles about the use and abuse of FreeAndNil but there is one point I really don’t like about FreeAndNil: It is not type-safe. Let’s have a look at the implementation:

The comment describes the issue: “Be careful to only pass TObjects to this routine.” That’s why I would implement it in a different way:

This code is type-safe and don’t use pointer arithmetic. Maybe I will ask Marco if there is a chance to add the generic FreeAndNil method directly to TObject.

This entry was posted in Delphi, RAD Studio 10.2 Tokyo, Tips and Tricks, Uncategorized. Bookmark the permalink.