{"id":17,"date":"2012-10-06T15:03:10","date_gmt":"2012-10-06T13:03:10","guid":{"rendered":"http:\/\/blog.kassebaum.eu\/?p=17"},"modified":"2012-10-09T09:21:26","modified_gmt":"2012-10-09T07:21:26","slug":"try-finally-for-multiple-objects","status":"publish","type":"post","link":"https:\/\/www.kassebaum.eu\/blog\/2012\/10\/06\/try-finally-for-multiple-objects\/","title":{"rendered":"try&#8230;finally for multiple objects"},"content":{"rendered":"<p>Today I found another common mistake. Someone used more than one object and has to free them.<\/p>\n<pre class=\"lang:delphi decode:true\">var\r\n  pObject1: TMyObject1;\r\n  pObject2: TMyObject2;\r\nbegin\r\n  pObject1 := TMyObject1.Create;\r\n  try\r\n    pObject1.DoSomething;\r\n    pObject2 := TMyObject2.Create;\r\n    try\r\n      pObject2.DoSomethingElse;\r\n    finally\r\n      pObject2.Free;\r\n    end;\r\n  finally\r\n    pObject1.Free;\r\n  end;\r\nend;<\/pre>\n<p>Instead of nested try&#8230;finally blocks it is easier and more readable to do the same with one try&#8230;finally block<\/p>\n<pre class=\"lang:delphi decode:true\">var\r\n  pObject1: TMyObject1;\r\n  pObject2: TMyObject2;\r\nbegin\r\n  pObject2 := nil;\r\n  pObject1 := nil;\r\n  try\r\n    pObject1 := TMyObject1.Create;\r\n    pObject1.DoSomething;\r\n    pObject2 := TMyObject2.Create;\r\n    pObject2.DoSomethingElse;\r\n  finally\r\n    pObject1.Free;\r\n    pObject2.Free;\r\n  end;\r\nend;<\/pre>\n<p>This solution can still be improved. The first object can be created outside the try&#8230;finally block<\/p>\n<pre class=\"lang:delphi decode:true\">var\r\n  pObject1: TMyObject1;\r\n  pObject2: TMyObject2;\r\nbegin\r\n  pObject2 := nil;\r\n  pObject1 := TMyObject1.Create;\r\n  try\r\n    pObject1.DoSomething;\r\n    pObject2 := TMyObject2.Create;\r\n    pObject2.DoSomethingElse;\r\n  finally\r\n    pObject1.Free;\r\n    pObject2.Free;\r\n  end;\r\nend;<\/pre>\n<p>Please remember that all variables that are freed in the finally block must be initialized before the try statement.<\/p>\n<p>I guess that most of you know this practice for many years but there are still people around who don&#8217;t.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I found another common mistake. Someone used more than one object and has to free them. var pObject1: TMyObject1; pObject2: TMyObject2; begin pObject1 := TMyObject1.Create; try pObject1.DoSomething; pObject2 := TMyObject2.Create; try pObject2.DoSomethingElse; finally pObject2.Free; end; finally pObject1.Free; end; end; &hellip; <a href=\"https:\/\/www.kassebaum.eu\/blog\/2012\/10\/06\/try-finally-for-multiple-objects\/\">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":[3],"tags":[5],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-tipsandtricks","tag-delphi"],"_links":{"self":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/17","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=17"}],"version-history":[{"count":7,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":51,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions\/51"}],"wp:attachment":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}