<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Don&#8217;t abuse FreeAndNil anymore</title>
	<atom:link href="http://delphitools.info/2010/02/06/dont-abuse-freeandnil-anymore/feed/" rel="self" type="application/rss+xml" />
	<link>http://delphitools.info/2010/02/06/dont-abuse-freeandnil-anymore/</link>
	<description>SamplingProfiler and other Delphi tools</description>
	<lastBuildDate>Mon, 06 Sep 2010 06:51:08 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: michaeltao</title>
		<link>http://delphitools.info/2010/02/06/dont-abuse-freeandnil-anymore/comment-page-1/#comment-1177</link>
		<dc:creator>michaeltao</dc:creator>
		<pubDate>Mon, 19 Jul 2010 08:25:47 +0000</pubDate>
		<guid isPermaLink="false">http://delphitools.info/?p=433#comment-1177</guid>
		<description>I had the same problem when I used freeandnil after close a window.I will string list from a dialog and set them to a listview and then I freeandnil this dialog instance. sometimes, the first row in listview will lose some text. 
It is fuck ..... I replace freeandnil with free, the problem  disappeared</description>
		<content:encoded><![CDATA[<p>I had the same problem when I used freeandnil after close a window.I will string list from a dialog and set them to a listview and then I freeandnil this dialog instance. sometimes, the first row in listview will lose some text.<br />
It is fuck &#8230;.. I replace freeandnil with free, the problem  disappeared</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bart van der Werf</title>
		<link>http://delphitools.info/2010/02/06/dont-abuse-freeandnil-anymore/comment-page-1/#comment-662</link>
		<dc:creator>Bart van der Werf</dc:creator>
		<pubDate>Fri, 12 Feb 2010 12:53:21 +0000</pubDate>
		<guid isPermaLink="false">http://delphitools.info/?p=433#comment-662</guid>
		<description>Alternatively something we do is hook FreeInstance to change the v-table pointer on the heapobject just before the FreeMemory call inside it.
This causes all virtual method calls on the now freed object to throw a CalledAMethodOnAFreedObject exception when the memory hasn&#039;t yet been reused or pageprotected.
The overhead is neglectible and works without replacing .Free or FreeAndNil calls.</description>
		<content:encoded><![CDATA[<p>Alternatively something we do is hook FreeInstance to change the v-table pointer on the heapobject just before the FreeMemory call inside it.<br />
This causes all virtual method calls on the now freed object to throw a CalledAMethodOnAFreedObject exception when the memory hasn&#8217;t yet been reused or pageprotected.<br />
The overhead is neglectible and works without replacing .Free or FreeAndNil calls.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric</title>
		<link>http://delphitools.info/2010/02/06/dont-abuse-freeandnil-anymore/comment-page-1/#comment-661</link>
		<dc:creator>Eric</dc:creator>
		<pubDate>Mon, 08 Feb 2010 07:31:29 +0000</pubDate>
		<guid isPermaLink="false">http://delphitools.info/?p=433#comment-661</guid>
		<description>&gt;NIL’ing the reference can also be valid in it’s own right.

Indeed, this why there is a need to distinguish between valid scenarios and abuses.

&gt;In a destructor, a partially destructed object may conceivably be re-destroyed.

I strongly disagree there: yes, some object reference can be FreeAndNil&#039;ed multiple times during as part of their design during an owner context lifetime, but that should never happen during a destruction chain. If it does happen, then you&#039;re facing either redundant or spaghetti code, with poor maintainability and higher risk of divergence.

Separating FreeAndNil and FreeAndInvalidate allows to make that distinction between a normal nil reference situation, and an abnormal one.

&gt; Any objects Free’d during execution of the previous partial destructor chain obviously should not be re-freed.

Indeed, but FreeAndNil is not a protection against that: a field will stay nil ONLY if the memory of the context that hosted the reference isn&#039;t reallocated during the destruction chain, something you just can&#039;t guarantee in a destruction chain (especially in multi-threading situations). 
This can (and does) lead to code initially &quot;working&quot; in single-threaded situations, that very randomly goes bonkers in multi-threading situations, or after some allocations get mixed in the destruction chain ...like allocations arising from debug code added to diagnose the very random bonkers situations just mentioned!

&gt;More realistically there may be “instantiate on demand” references

In that case use FreeAndNil, that&#039;s what it&#039;s for.
For all the other situations (IME the vast majority of cases), there is no need to abuse FreeAndNil, and one should never forget that FreeAndNil/FreeAndInvalidate are nothing more than optimistic defensive strategies, so fix the design, that saves pains down the road. :)</description>
		<content:encoded><![CDATA[<p>>NIL’ing the reference can also be valid in it’s own right.</p>
<p>Indeed, this why there is a need to distinguish between valid scenarios and abuses.</p>
<p>>In a destructor, a partially destructed object may conceivably be re-destroyed.</p>
<p>I strongly disagree there: yes, some object reference can be FreeAndNil&#8217;ed multiple times during as part of their design during an owner context lifetime, but that should never happen during a destruction chain. If it does happen, then you&#8217;re facing either redundant or spaghetti code, with poor maintainability and higher risk of divergence.</p>
<p>Separating FreeAndNil and FreeAndInvalidate allows to make that distinction between a normal nil reference situation, and an abnormal one.</p>
<p>> Any objects Free’d during execution of the previous partial destructor chain obviously should not be re-freed.</p>
<p>Indeed, but FreeAndNil is not a protection against that: a field will stay nil ONLY if the memory of the context that hosted the reference isn&#8217;t reallocated during the destruction chain, something you just can&#8217;t guarantee in a destruction chain (especially in multi-threading situations).<br />
This can (and does) lead to code initially &#8220;working&#8221; in single-threaded situations, that very randomly goes bonkers in multi-threading situations, or after some allocations get mixed in the destruction chain &#8230;like allocations arising from debug code added to diagnose the very random bonkers situations just mentioned!</p>
<p>>More realistically there may be “instantiate on demand” references</p>
<p>In that case use FreeAndNil, that&#8217;s what it&#8217;s for.<br />
For all the other situations (IME the vast majority of cases), there is no need to abuse FreeAndNil, and one should never forget that FreeAndNil/FreeAndInvalidate are nothing more than optimistic defensive strategies, so fix the design, that saves pains down the road. <img src='http://delphitools.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rudy Velthuis</title>
		<link>http://delphitools.info/2010/02/06/dont-abuse-freeandnil-anymore/comment-page-1/#comment-660</link>
		<dc:creator>Rudy Velthuis</dc:creator>
		<pubDate>Sun, 07 Feb 2010 21:31:34 +0000</pubDate>
		<guid isPermaLink="false">http://delphitools.info/?p=433#comment-660</guid>
		<description>&#124; thus leaving a invalid reference that should however never be used
&#124; anymore when the code design is correct

I would say: &quot;thus leaving an invalid reference that CAN however never be used anymore.&quot; This does not require FreeAndNil, nor does it need anything like FreeAndInvalidate, IMO.</description>
		<content:encoded><![CDATA[<p>| thus leaving a invalid reference that should however never be used<br />
| anymore when the code design is correct</p>
<p>I would say: &#8220;thus leaving an invalid reference that CAN however never be used anymore.&#8221; This does not require FreeAndNil, nor does it need anything like FreeAndInvalidate, IMO.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jolyon Smith</title>
		<link>http://delphitools.info/2010/02/06/dont-abuse-freeandnil-anymore/comment-page-1/#comment-659</link>
		<dc:creator>Jolyon Smith</dc:creator>
		<pubDate>Sun, 07 Feb 2010 19:56:00 +0000</pubDate>
		<guid isPermaLink="false">http://delphitools.info/?p=433#comment-659</guid>
		<description>Sorry, but this conflates two problems and discards a perfectly valid strategy simply because it only addresses ONE of those problems.

FreeAndNIL() isn&#039;t always about NIL&#039;ing a reference to an object and hoping that an AV will result if that reference is subsequently re-used.  NIL&#039;ing the reference can also be valid in it&#039;s own right.

In a destructor, a partially destructed object may conceivably be re-destroyed.  Any objects Free&#039;d during execution of the previous partial destructor chain obviously should not be re-freed.

Yes, a partially destroyed object indicates a potential problem in that class or it&#039;s design, but still may be a valid/unavoidable scenario.

More realistically there may be &quot;instantiate on demand&quot; references that are periodically disposed of, but not replaced until needed.  NIL&#039;ing those references when disposed creates a testable condition to trigger the creation of a new object if subsequently referenced.

This sort of thinking process coming out of Embarcadero is illuminating.  It seems to indicate to me that the guys &quot;running the show&quot; behind Delphi these days are less concerned about practical work and getting a job of work done, and more interested in abstract language and coding theory and practises.

Sad.</description>
		<content:encoded><![CDATA[<p>Sorry, but this conflates two problems and discards a perfectly valid strategy simply because it only addresses ONE of those problems.</p>
<p>FreeAndNIL() isn&#8217;t always about NIL&#8217;ing a reference to an object and hoping that an AV will result if that reference is subsequently re-used.  NIL&#8217;ing the reference can also be valid in it&#8217;s own right.</p>
<p>In a destructor, a partially destructed object may conceivably be re-destroyed.  Any objects Free&#8217;d during execution of the previous partial destructor chain obviously should not be re-freed.</p>
<p>Yes, a partially destroyed object indicates a potential problem in that class or it&#8217;s design, but still may be a valid/unavoidable scenario.</p>
<p>More realistically there may be &#8220;instantiate on demand&#8221; references that are periodically disposed of, but not replaced until needed.  NIL&#8217;ing those references when disposed creates a testable condition to trigger the creation of a new object if subsequently referenced.</p>
<p>This sort of thinking process coming out of Embarcadero is illuminating.  It seems to indicate to me that the guys &#8220;running the show&#8221; behind Delphi these days are less concerned about practical work and getting a job of work done, and more interested in abstract language and coding theory and practises.</p>
<p>Sad.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric</title>
		<link>http://delphitools.info/2010/02/06/dont-abuse-freeandnil-anymore/comment-page-1/#comment-658</link>
		<dc:creator>Eric</dc:creator>
		<pubDate>Sun, 07 Feb 2010 13:14:51 +0000</pubDate>
		<guid isPermaLink="false">http://delphitools.info/?p=433#comment-658</guid>
		<description>It&#039;s no magic bullet, and they could do it (and likely will), sure, but then it can be pointed to as an explicit design error, while using FreeAndNil / Assigned aren&#039;t by themselves explicit errors.
Nil is a &quot;valid&quot; value for an object reference, and with Assigned(), there are valid design and usage case scenarios. There exists no such scenario for an AssignedInvalid function, so you can shoot on sight whoever comes up with one, something you can&#039;t do for FreeAndNil/Assigned.

It&#039;s all about moving the issue from a gray area, to one of black and white.</description>
		<content:encoded><![CDATA[<p>It&#8217;s no magic bullet, and they could do it (and likely will), sure, but then it can be pointed to as an explicit design error, while using FreeAndNil / Assigned aren&#8217;t by themselves explicit errors.<br />
Nil is a &#8220;valid&#8221; value for an object reference, and with Assigned(), there are valid design and usage case scenarios. There exists no such scenario for an AssignedInvalid function, so you can shoot on sight whoever comes up with one, something you can&#8217;t do for FreeAndNil/Assigned.</p>
<p>It&#8217;s all about moving the issue from a gray area, to one of black and white.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian Wimmer</title>
		<link>http://delphitools.info/2010/02/06/dont-abuse-freeandnil-anymore/comment-page-1/#comment-657</link>
		<dc:creator>Christian Wimmer</dc:creator>
		<pubDate>Sun, 07 Feb 2010 01:43:04 +0000</pubDate>
		<guid isPermaLink="false">http://delphitools.info/?p=433#comment-657</guid>
		<description>I think moving from memory address 0 (nil) to 1 is only moving the problem to another memory address. The next step people will do is to create an AssignedInvalid function (people tend to be pragmatic) that checks for such an invalid object. And history repeats itself.</description>
		<content:encoded><![CDATA[<p>I think moving from memory address 0 (nil) to 1 is only moving the problem to another memory address. The next step people will do is to create an AssignedInvalid function (people tend to be pragmatic) that checks for such an invalid object. And history repeats itself.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
