<?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/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
	xmlns:media="http://search.yahoo.com/mrss/"
	>
<channel>
	<title>Comments on: Weak vs Strong References in AS3</title>
	<atom:link href="http://dispatchevent.org/mims/creating-weak-references-in-as3/feed/" rel="self" type="application/rss+xml" />
	<link>http://dispatchevent.org/mims/creating-weak-references-in-as3/</link>
	<description>Collective thoughts of the New York Flash community</description>
	<lastBuildDate>Sat, 04 Jul 2009 20:08:45 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Garbage Collector and Dictionary</title>
		<link>http://dispatchevent.org/mims/creating-weak-references-in-as3/comment-page-1/#comment-120276</link>
		<dc:creator>Garbage Collector and Dictionary</dc:creator>
		<pubDate>Thu, 17 Jul 2008 23:59:18 +0000</pubDate>
		<guid isPermaLink="false">http://mimswright.com/blog/?p=213#comment-120276</guid>
		<description>[...] Weak references are cool. No. they are substantial. I don&#8217;t know how we lived without them before. Anyway, they made our (we = flash developers) life better. I use them very often, when adding event listeners or caching with dictionaries (and I&#8217;m sure I&#8217;d use them even more often if there was any more possibilty provided). [...]</description>
		<content:encoded><![CDATA[<p>[...] Weak references are cool. No. they are substantial. I don&#8217;t know how we lived without them before. Anyway, they made our (we = flash developers) life better. I use them very often, when adding event listeners or caching with dictionaries (and I&#8217;m sure I&#8217;d use them even more often if there was any more possibilty provided). [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Schell</title>
		<link>http://dispatchevent.org/mims/creating-weak-references-in-as3/comment-page-1/#comment-64605</link>
		<dc:creator>Schell</dc:creator>
		<pubDate>Sun, 16 Dec 2007 13:17:26 +0000</pubDate>
		<guid isPermaLink="false">http://mimswright.com/blog/?p=213#comment-64605</guid>
		<description>Yes, I see that my toString() wouldn&#039;t trace the list of functions in the order that they were listed, but without adding another variable to keep track of the order added, how would you trace or execute them first come first serve? It seems like the order it traces in is the same order it executes in. 

Another note: the interesting thing about this class isn&#039;t how the methods are replaced, but because (contrary to posts I had read regarding the matter -&gt; http://gskinner.com/blog/archives/2006/07/as3_dictionary.html) I couldn&#039;t create duplicate function entries with the same method (in a weakly referenced Dictionary). 

My main point was that changing the Dictionary&#039;s reference (weak or strong) changes the output of the code I posted on your blog. For example, changing the code I had posted above to instantiate my Queue class with a &#039;strong&#039; referenced Dictionary, the output is as follows:

object [Queue]
-&gt;	function Function() {}  3,5
-&gt;	function Function() {}  4,5
8
9
object [Queue]
done

As you can see calling toString() on the class traces the contents (in whatever order it decides) and then executes the functions in that same order. Instantiating the class with a weakly referenced Dictionary results in the following:

object [Queue]
-&gt;	function Function() {}  4,5
-&gt;	function Function() {}  3,5
object [Queue]
done

Here you can see that immediately after pushing the functions into the Queue, the functions can be listed, but after the TimerEvent.TIMER (which is set to 1 millisecond) has triggered, the functions that were once listed are no longer there! 

I may be missing the point, but I never explicitly removed the functions from the queue, yet they disappeared. When the class is instantiated with a strong referenced Dictionary the functions last and are executed. I don&#039;t know what else to attribute this behavior to besides the GC. Maybe you can help me understand this more clearly. 

On a last note, my class is supposed to handle requests from containing classes or extending classes (or the main timeline) that would like to call functions (such as a resize) before those functions are necessarily ready, these functions are most definitely static properties (like a setter function) and declaring dynamic functions aren&#039;t so much an option. One way around this would be to repeat the given function within itself as an alternative dynamic function to execute if the given function can&#039;t operate, passing the dynamic function into the queue, but that seems like it might be complicating the matter... Thanks for pounding this out with me!</description>
		<content:encoded><![CDATA[<p>Yes, I see that my toString() wouldn&#8217;t trace the list of functions in the order that they were listed, but without adding another variable to keep track of the order added, how would you trace or execute them first come first serve? It seems like the order it traces in is the same order it executes in. </p>
<p>Another note: the interesting thing about this class isn&#8217;t how the methods are replaced, but because (contrary to posts I had read regarding the matter -&gt; <a href="http://gskinner.com/blog/archives/2006/07/as3_dictionary.html)" rel="nofollow">http://gskinner.com/blog/archives/2006/07/as3_dictionary.html)</a> I couldn&#8217;t create duplicate function entries with the same method (in a weakly referenced Dictionary). </p>
<p>My main point was that changing the Dictionary&#8217;s reference (weak or strong) changes the output of the code I posted on your blog. For example, changing the code I had posted above to instantiate my Queue class with a &#8217;strong&#8217; referenced Dictionary, the output is as follows:</p>
<p>object [Queue]<br />
-&gt;	function Function() {}  3,5<br />
-&gt;	function Function() {}  4,5<br />
8<br />
9<br />
object [Queue]<br />
done</p>
<p>As you can see calling toString() on the class traces the contents (in whatever order it decides) and then executes the functions in that same order. Instantiating the class with a weakly referenced Dictionary results in the following:</p>
<p>object [Queue]<br />
-&gt;	function Function() {}  4,5<br />
-&gt;	function Function() {}  3,5<br />
object [Queue]<br />
done</p>
<p>Here you can see that immediately after pushing the functions into the Queue, the functions can be listed, but after the TimerEvent.TIMER (which is set to 1 millisecond) has triggered, the functions that were once listed are no longer there! </p>
<p>I may be missing the point, but I never explicitly removed the functions from the queue, yet they disappeared. When the class is instantiated with a strong referenced Dictionary the functions last and are executed. I don&#8217;t know what else to attribute this behavior to besides the GC. Maybe you can help me understand this more clearly. </p>
<p>On a last note, my class is supposed to handle requests from containing classes or extending classes (or the main timeline) that would like to call functions (such as a resize) before those functions are necessarily ready, these functions are most definitely static properties (like a setter function) and declaring dynamic functions aren&#8217;t so much an option. One way around this would be to repeat the given function within itself as an alternative dynamic function to execute if the given function can&#8217;t operate, passing the dynamic function into the queue, but that seems like it might be complicating the matter&#8230; Thanks for pounding this out with me!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mims Wright</title>
		<link>http://dispatchevent.org/mims/creating-weak-references-in-as3/comment-page-1/#comment-64208</link>
		<dc:creator>Mims Wright</dc:creator>
		<pubDate>Fri, 14 Dec 2007 18:04:49 +0000</pubDate>
		<guid isPermaLink="false">http://mimswright.com/blog/?p=213#comment-64208</guid>
		<description>Hi Schell, 

I was looking at the code you posted to my site. I wanted to make a few notes that may help out.

First, the order of the trace has to do with the for...in loop that you use in the toString() method. This loop doesn&#039;t necessarily output in the order you added the functions.

Second, this isn&#039;t happening as a result of weak references. As you mentioned on your blog post, you cannot use the same function twice as a key without overwriting the previous value. In a Dictionary, the key object acts the same as an index for an array. If you had 

var a:Array = new Array();
a[0] = &quot;foo&quot;;
a[1] = &quot;bar&quot;;
a[1] = &quot;woot&quot;;
trace(a); // foo,woot

so it stands to reason that you shouldn&#039;t be able to do this with functions as keys either.

Since this class essentially runs a list of functions one after the other, you may want to try using dynamic Functions instead. Something like this:

var func:Function = new Function ():void {
    blah (1, 5);
    blah (2, 5);
    blah (4, 5);
    blah2(3, 5);
}
func();

I hope that was helpful.</description>
		<content:encoded><![CDATA[<p>Hi Schell, </p>
<p>I was looking at the code you posted to my site. I wanted to make a few notes that may help out.</p>
<p>First, the order of the trace has to do with the for&#8230;in loop that you use in the toString() method. This loop doesn&#8217;t necessarily output in the order you added the functions.</p>
<p>Second, this isn&#8217;t happening as a result of weak references. As you mentioned on your blog post, you cannot use the same function twice as a key without overwriting the previous value. In a Dictionary, the key object acts the same as an index for an array. If you had </p>
<p>var a:Array = new Array();<br />
a[0] = &#8220;foo&#8221;;<br />
a[1] = &#8220;bar&#8221;;<br />
a[1] = &#8220;woot&#8221;;<br />
trace(a); // foo,woot</p>
<p>so it stands to reason that you shouldn&#8217;t be able to do this with functions as keys either.</p>
<p>Since this class essentially runs a list of functions one after the other, you may want to try using dynamic Functions instead. Something like this:</p>
<p>var func:Function = new Function ():void {<br />
    blah (1, 5);<br />
    blah (2, 5);<br />
    blah (4, 5);<br />
    blah2(3, 5);<br />
}<br />
func();</p>
<p>I hope that was helpful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Schell</title>
		<link>http://dispatchevent.org/mims/creating-weak-references-in-as3/comment-page-1/#comment-63193</link>
		<dc:creator>Schell</dc:creator>
		<pubDate>Mon, 10 Dec 2007 06:55:47 +0000</pubDate>
		<guid isPermaLink="false">http://mimswright.com/blog/?p=213#comment-63193</guid>
		<description>Weakly referenced Dictionary objects are great but be careful using them to store methods. There&#039;s a glitch in the way Dictionaries resolve references to methods that causes a weakly referenced Dictionary to allow the method to be GC&#039;d immediately. While writing a Queue class I found a nice example. My class can be found here -&gt; &lt;a href=&quot;http://blog.efnx.com/?p=37&quot; rel=&quot;nofollow&quot;&gt;blog.efnx.com/?p=37&lt;/a&gt; and this is the code to use in your main timeline to recreate the action.
&lt;code&gt;
function blah(val1:int, val2:int):void
{
 trace(val1+val2);
}

function blah2(val1:int, val2:int):void
{
 trace(val1+val2);
}

var queue:Queue = new Queue(true);   
 queue.push(blah, 1, 5);
 queue.push(blah, 2, 5);
 queue.push(blah, 4, 5);
 queue.push(blah2, 3, 5);

trace(queue);

var timer:Timer = new Timer(1, 1);
timer.addEventListener(TimerEvent.TIMER, startQueue, false, 0, true);
timer.start();

function startQueue(event:TimerEvent):void
{
 queue.start();
 trace(queue);
 trace(&quot;done&quot;);
}</description>
		<content:encoded><![CDATA[<p>Weakly referenced Dictionary objects are great but be careful using them to store methods. There&#8217;s a glitch in the way Dictionaries resolve references to methods that causes a weakly referenced Dictionary to allow the method to be GC&#8217;d immediately. While writing a Queue class I found a nice example. My class can be found here -&gt; <a href="http://blog.efnx.com/?p=37" rel="nofollow">blog.efnx.com/?p=37</a> and this is the code to use in your main timeline to recreate the action.<br />
<code><br />
function blah(val1:int, val2:int):void<br />
{<br />
 trace(val1+val2);<br />
}</p>
<p>function blah2(val1:int, val2:int):void<br />
{<br />
 trace(val1+val2);<br />
}</p>
<p>var queue:Queue = new Queue(true);<br />
 queue.push(blah, 1, 5);<br />
 queue.push(blah, 2, 5);<br />
 queue.push(blah, 4, 5);<br />
 queue.push(blah2, 3, 5);</p>
<p>trace(queue);</p>
<p>var timer:Timer = new Timer(1, 1);<br />
timer.addEventListener(TimerEvent.TIMER, startQueue, false, 0, true);<br />
timer.start();</p>
<p>function startQueue(event:TimerEvent):void<br />
{<br />
 queue.start();<br />
 trace(queue);<br />
 trace("done");<br />
}</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mims Wright</title>
		<link>http://dispatchevent.org/mims/creating-weak-references-in-as3/comment-page-1/#comment-63120</link>
		<dc:creator>Mims Wright</dc:creator>
		<pubDate>Sun, 09 Dec 2007 22:33:55 +0000</pubDate>
		<guid isPermaLink="false">http://mimswright.com/blog/?p=213#comment-63120</guid>
		<description>adampasz Asked: &quot;What are some examples of when a Weak Reference might be useful?&quot;

Adam, I&#039;ve added additional text to the above post to answer your question.</description>
		<content:encoded><![CDATA[<p>adampasz Asked: &#8220;What are some examples of when a Weak Reference might be useful?&#8221;</p>
<p>Adam, I&#8217;ve added additional text to the above post to answer your question.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: adampasz</title>
		<link>http://dispatchevent.org/mims/creating-weak-references-in-as3/comment-page-1/#comment-63032</link>
		<dc:creator>adampasz</dc:creator>
		<pubDate>Sun, 09 Dec 2007 16:03:39 +0000</pubDate>
		<guid isPermaLink="false">http://mimswright.com/blog/?p=213#comment-63032</guid>
		<description>What are some examples of when a Weak Reference might be useful?</description>
		<content:encoded><![CDATA[<p>What are some examples of when a Weak Reference might be useful?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
