<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.4" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: C# WeakReference Example</title>
	<link>http://www.robherbst.com/blog/2006/08/21/c-weakreference-example/</link>
	<description></description>
	<pubDate>Wed, 07 Jan 2009 04:08:01 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.4</generator>

	<item>
		<title>by: Michael</title>
		<link>http://www.robherbst.com/blog/2006/08/21/c-weakreference-example/#comment-557</link>
		<pubDate>Mon, 29 Jan 2007 21:43:29 +0000</pubDate>
		<guid>http://www.robherbst.com/blog/2006/08/21/c-weakreference-example/#comment-557</guid>
					<description>The other place you need a WeakReference is in circular data structures. For example, a circularly linked list will leak memory unless at least one of the References is weak. Or a simpler example is if A -&#62; B and B -&#62; A one must be weak.</description>
		<content:encoded><![CDATA[<p>The other place you need a WeakReference is in circular data structures. For example, a circularly linked list will leak memory unless at least one of the References is weak. Or a simpler example is if A -&gt; B and B -&gt; A one must be weak.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Anonmyous</title>
		<link>http://www.robherbst.com/blog/2006/08/21/c-weakreference-example/#comment-555</link>
		<pubDate>Thu, 25 Jan 2007 14:05:18 +0000</pubDate>
		<guid>http://www.robherbst.com/blog/2006/08/21/c-weakreference-example/#comment-555</guid>
					<description>Let me explain my point;

For me, a weak-reference is really usefull for Events.

Imagine you have a longliving objects, which has severeal interessting events for you.

Imagine a class/object that ist not that long living. So if you registerd a event from the "long-living" object, your short living object would not be garbagecollectet because it holds a "strong" ref. You have to unregister the event, for making your short-living object ready to be G-collected.

If there were only weak references, the G-Collector could say, fine your "short-living" class has only weakers so i G-Collect it.

That is my big advantage of Weakreferences. (I dont see the point from translating weakers to strongers, because i have to decide in my implemenation if my short-living object would "keep" alive or not.)

(Im just learning and try to unterstand your listened advantages).

RealWorld example;

Your "Car" Object listens to a global "GPS-Satellite" object and wants to now when he bypasses the border of the country. (the Country_Changed event ;-)

So if nobody uses this car, the car has not to listen to the satellite (becuase its useless).

If the car would "weakly" listen to this event, and there is nobody who is using this car (using this car would mean there is a strong ref to it), he could be G-Collected (whatever this means in this sample).

If he does "strongly" listen to the satelite, he makes it self "unCollectable". --&#62; Untill the GPS-Satellite would be destroyed itself which should not happen that often.

(You cant bash, destroy stomp the car) which will cause memory-leaks.

Hm

Greets

Anonmyous</description>
		<content:encoded><![CDATA[<p>Let me explain my point;</p>
<p>For me, a weak-reference is really usefull for Events.</p>
<p>Imagine you have a longliving objects, which has severeal interessting events for you.</p>
<p>Imagine a class/object that ist not that long living. So if you registerd a event from the &#8220;long-living&#8221; object, your short living object would not be garbagecollectet because it holds a &#8220;strong&#8221; ref. You have to unregister the event, for making your short-living object ready to be G-collected.</p>
<p>If there were only weak references, the G-Collector could say, fine your &#8220;short-living&#8221; class has only weakers so i G-Collect it.</p>
<p>That is my big advantage of Weakreferences. (I dont see the point from translating weakers to strongers, because i have to decide in my implemenation if my short-living object would &#8220;keep&#8221; alive or not.)</p>
<p>(Im just learning and try to unterstand your listened advantages).</p>
<p>RealWorld example;</p>
<p>Your &#8220;Car&#8221; Object listens to a global &#8220;GPS-Satellite&#8221; object and wants to now when he bypasses the border of the country. (the Country_Changed event <img src='http://www.robherbst.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>So if nobody uses this car, the car has not to listen to the satellite (becuase its useless).</p>
<p>If the car would &#8220;weakly&#8221; listen to this event, and there is nobody who is using this car (using this car would mean there is a strong ref to it), he could be G-Collected (whatever this means in this sample).</p>
<p>If he does &#8220;strongly&#8221; listen to the satelite, he makes it self &#8220;unCollectable&#8221;. &#8211;&gt; Untill the GPS-Satellite would be destroyed itself which should not happen that often.</p>
<p>(You cant bash, destroy stomp the car) which will cause memory-leaks.</p>
<p>Hm</p>
<p>Greets</p>
<p>Anonmyous
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Anonmyous</title>
		<link>http://www.robherbst.com/blog/2006/08/21/c-weakreference-example/#comment-282</link>
		<pubDate>Fri, 15 Dec 2006 14:27:48 +0000</pubDate>
		<guid>http://www.robherbst.com/blog/2006/08/21/c-weakreference-example/#comment-282</guid>
					<description>"This can be very useful if you are trying to managing the lifetime of objects in a cache or adding optimizations to possibly save time by not having to recreate an expensive object."

Hmm i think a weak reference would not prevent you to recreate a G-collected object.

From the other post you describe.

"If the user were to open that screen up again, you could check the Target property of the WeakReference to see if the large object has been garbage collected yet. If the Target property is not null, you can create a new “strong reference” by setting a normal".

If the target property is not null, there was an existing strong ref, and the weak-ref was useless because the object would not be garbage collected anyway. If the target property is null, your object has been collected and you have to recreate your object.

"You might keep a WeakReference around after you set the “strong reference” to the object to null when the user cancel’s out of the screen."

If you set the strong reference to null, and you have a weakreference to it, the object is garbage collected.

So wheres the advantage in your example?</description>
		<content:encoded><![CDATA[<p>&#8220;This can be very useful if you are trying to managing the lifetime of objects in a cache or adding optimizations to possibly save time by not having to recreate an expensive object.&#8221;</p>
<p>Hmm i think a weak reference would not prevent you to recreate a G-collected object.</p>
<p>From the other post you describe.</p>
<p>&#8220;If the user were to open that screen up again, you could check the Target property of the WeakReference to see if the large object has been garbage collected yet. If the Target property is not null, you can create a new “strong reference” by setting a normal&#8221;.</p>
<p>If the target property is not null, there was an existing strong ref, and the weak-ref was useless because the object would not be garbage collected anyway. If the target property is null, your object has been collected and you have to recreate your object.</p>
<p>&#8220;You might keep a WeakReference around after you set the “strong reference” to the object to null when the user cancel’s out of the screen.&#8221;</p>
<p>If you set the strong reference to null, and you have a weakreference to it, the object is garbage collected.</p>
<p>So wheres the advantage in your example?
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Chris Lasater</title>
		<link>http://www.robherbst.com/blog/2006/08/21/c-weakreference-example/#comment-22</link>
		<pubDate>Tue, 29 Aug 2006 05:01:06 +0000</pubDate>
		<guid>http://www.robherbst.com/blog/2006/08/21/c-weakreference-example/#comment-22</guid>
					<description>Nice article. Wasn't as familiar with garbage collection via weakrefrences as I would have liked and this explained it better than msdn. Thanks!</description>
		<content:encoded><![CDATA[<p>Nice article. Wasn&#8217;t as familiar with garbage collection via weakrefrences as I would have liked and this explained it better than msdn. Thanks!
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
