<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Richard Lord &#187; Weak reference</title>
	<atom:link href="http://www.richardlord.net/blog/tag/weak-reference/feed" rel="self" type="application/rss+xml" />
	<link>http://www.richardlord.net</link>
	<description>Actionscript/Flex, PHP and Java developer</description>
	<lastBuildDate>Mon, 23 Jan 2012 17:07:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Different types of weak references</title>
		<link>http://www.richardlord.net/blog/different-types-of-weak-references</link>
		<comments>http://www.richardlord.net/blog/different-types-of-weak-references#comments</comments>
		<pubDate>Wed, 15 Aug 2007 13:33:53 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Free code]]></category>
		<category><![CDATA[Weak reference]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/different-types-of-weak-references/</guid>
		<description><![CDATA[<p>Actionscript 3 seems to have two different types of weak references. The difference relates to method closures (or bound methods) and the weak references used by the EventDispatcher and Dictionary classes. Try the following...</p>]]></description>
			<content:encoded><![CDATA[<p>Actionscript 3 seems to have two different types of weak references. The difference relates to method closures (or bound methods) and the weak references used by the EventDispatcher and Dictionary classes. Try the following</p>

<pre class="code">package
{
   import flash.display.*;
   import flash.utils.Dictionary;
   import flash.events.MouseEvent;
   
   public class Test extends Sprite
   {
      private var dic:Dictionary;
      private var store:Function;
      
      public function Test() {
         var listen:Sprite = new Sprite();
         listen.graphics.beginFill( 0xFF0000 );
         listen.graphics.drawCircle( 50, 50, 50 );
         addChild( listen );

         dic = new Dictionary( true );
         dic[inDictionary] = 1;
         dic[inDictionaryAndProperty] = 1;
         store = inDictionaryAndProperty;
         
         listen.addEventListener( MouseEvent.CLICK, doClick );
         listen.addEventListener( MouseEvent.CLICK, 
                             inEventListeners, false, 0, true );
      }
        
      public function doClick( ev:MouseEvent ):void {
         for( var fn:Object in dic ) {
            fn( ev );
         }
      }

      public function inDictionary( ev:MouseEvent ):void {
         trace( "method in dictionary" );
      }

      public function inDictionaryAndProperty( ev:MouseEvent ):void {
         trace( "method in dictionary and property" );
      }
        
      public function inEventListeners( ev:MouseEvent ):void {
         trace( "method in event listeners" );
      }
   }
}</pre>

<p>The output is</p>

<pre class="code">method in dictionary and property
method in event listeners</pre>

<p>The EventDispatcher retains the weak reference to the method closure (inEventListeners) because the object it&#8217;s bound to still exists. It will drop its reference to the method closure when the bound object no longer exists.</p>

<p>The Dictionary, however, doesn&#8217;t retain the weak reference to the method closure (inDictionary), despite the fact that the method still exists (it exists in the class) and the object it is bound to still exists.</p>

<p>If, however, you have another, strong, reference to the same method closure (inDictionaryAndProperty), then it isn&#8217;t garbage collected and the Dictionary retains its reference to it.</p>

<p>So the EventDispatcher and Dictionary treat weak references to method closures differently.</p>

<p>It seems to me that the Dictionary takes a purist view &#8211; it holds the only reference to the method closure so it can be garbage collected &#8211; while the EventDispatcher takes a practical view &#8211; the method and the bound object both exist so the method closure should be retained.</p>

<p>Why the difference? I&#8217;d love to know.</p>]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/different-types-of-weak-references/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating weak references in Actionscript 3</title>
		<link>http://www.richardlord.net/blog/create-your-own-weak-references-in-actionscript-3</link>
		<comments>http://www.richardlord.net/blog/create-your-own-weak-references-in-actionscript-3#comments</comments>
		<pubDate>Wed, 15 Aug 2007 10:07:17 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Free code]]></category>
		<category><![CDATA[Weak reference]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/create-your-own-weak-references-in-actionscript-3/</guid>
		<description><![CDATA[<p>A weak reference is a reference that does not prevent the object from being garbage collected. It's great that Actionscript 3 has weak references in the EventDispatcher and Dictionary classes. But sometimes you want to create your own weak references. The source code is very short...</p>
]]></description>
			<content:encoded><![CDATA[<p>A weak reference is a reference that does not prevent the object from being garbage collected. It&#8217;s great that Actionscript 3 has weak references in the EventDispatcher and Dictionary classes. But sometimes you want to create your own weak references. Something along the lines of</p>

<pre class="code">var weak:WeakRef = new WeakRef( someObject );</pre>

<p>Well, there is a way. It&#8217;s a bit of a hack, and unfortunately uses a full Dictionary object for each weak reference, but it works. The source code is on <a href="https://github.com/richardlord/Actionscript-Toolkit/blob/master/src/net/richardlord/utils/WeakRef.as">my public GitHub repository</a>.</p>

<p>To use the class</p>

<pre class="code">// Create a weak reference
var weak:Weakref = new WeakRef( obj );

// Later use the referenced object
var strong = weak.get();
if( strong != null )
{
    // use strong here
}
else
{
    // garbage collector has disposed of the object
}</pre>

<p>If the get method returns null then the object has been garbage collected.</p>

<p>The source is free under the terms of the MIT licence.</p>]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/create-your-own-weak-references-in-actionscript-3/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

