{"id":549,"date":"2017-04-06T00:11:52","date_gmt":"2017-04-05T22:11:52","guid":{"rendered":"http:\/\/blog.kassebaum.eu\/?p=549"},"modified":"2017-04-06T00:11:52","modified_gmt":"2017-04-05T22:11:52","slug":"freeandnil","status":"publish","type":"post","link":"https:\/\/www.kassebaum.eu\/blog\/2017\/04\/06\/freeandnil\/","title":{"rendered":"FreeAndNil"},"content":{"rendered":"<p>There are a lot of articles about the use and abuse of FreeAndNil but there is one point I really don&#8217;t like about FreeAndNil: It is not type-safe. Let&#8217;s have a look at the implementation:<\/p>\n<pre class=\"lang:delphi decode:true\">{ FreeAndNil frees the given TObject instance and sets the variable reference\r\n  to nil.  Be careful to only pass TObjects to this routine. }\r\n\r\nprocedure FreeAndNil(var Obj); inline;\r\n...\r\n\r\nprocedure FreeAndNil(var Obj);\r\n{$IF not Defined(AUTOREFCOUNT)}\r\nvar\r\n  Temp: TObject;\r\nbegin\r\n  Temp := TObject(Obj);\r\n  Pointer(Obj) := nil;\r\n  Temp.Free;\r\nend;\r\n{$ELSE}\r\nbegin\r\n  TObject(Obj) := nil;\r\nend;\r\n{$ENDIF}\r\n\r\n\r\n\r\n<\/pre>\n<p>The comment describes the issue: &#8220;Be careful to only pass TObjects to this routine.&#8221; That&#8217;s why I would implement it in a different way:<\/p>\n<pre class=\"lang:delphi decode:true \">type\r\n  TObjectHelper = class helper for TObject\r\n  public\r\n    class procedure FreeAndNil&lt;T: class&gt;(var AValue: T); inline; static;\r\n  end;\r\n\r\n{ TObjectHelper }\r\n\r\nclass procedure TObjectHelper.FreeAndNil&lt;T&gt;(var AValue: T);\r\n{$IF not Defined(AUTOREFCOUNT)}\r\nvar\r\n  lTemp: TObject;\r\nbegin\r\n  lTemp := AValue;\r\n  AValue := nil;\r\n  lTemp.Free;\r\nend;\r\n{$ELSE}\r\nbegin\r\n  AValue := nil;\r\nend;\r\n{$ENDIF}\r\n<\/pre>\n<p>This code is type-safe and don&#8217;t use pointer arithmetic. Maybe I will ask Marco if there is a chance to add the generic FreeAndNil method directly to TObject.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are a lot of articles about the use and abuse of FreeAndNil but there is one point I really don&#8217;t like about FreeAndNil: It is not type-safe. Let&#8217;s have a look at the implementation: { FreeAndNil frees the given &hellip; <a href=\"https:\/\/www.kassebaum.eu\/blog\/2017\/04\/06\/freeandnil\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,35,3,1],"tags":[],"class_list":["post-549","post","type-post","status-publish","format-standard","hentry","category-delphi","category-rad-studio-10-2-tokyo","category-tipsandtricks","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/comments?post=549"}],"version-history":[{"count":5,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/549\/revisions"}],"predecessor-version":[{"id":554,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/549\/revisions\/554"}],"wp:attachment":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/media?parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/categories?post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/tags?post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}