<?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>blog.betabong.com &#187; closure</title>
	<atom:link href="http://blog.betabong.com/tag/closure/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.betabong.com</link>
	<description></description>
	<lastBuildDate>Thu, 13 May 2010 09:49:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Weak Method Closure</title>
		<link>http://blog.betabong.com/2008/09/26/weak-method-closure/</link>
		<comments>http://blog.betabong.com/2008/09/26/weak-method-closure/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 23:36:16 +0000</pubDate>
		<dc:creator>betabong</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[weak]]></category>

		<guid isPermaLink="false">http://blog.betabong.com/?p=115</guid>
		<description><![CDATA[Sometimes you want to pass an objects function as an argument and store that function (along its arguments) for later calling. Still you don&#8217;t want that storage to prevent its object (instance) from being destroyed (from being removed by the garbage collector). But storing a function does so, because it holds a reference to the [...]


Related posts:<ol><li><a href='http://blog.betabong.com/2008/07/21/nested-inner-functions-can-be-evil/' rel='bookmark' title='Permanent Link: Nested inner functions can be evil'>Nested inner functions can be evil</a></li><li><a href='http://blog.betabong.com/2009/04/13/metatunnel-1k-demo-as-vs-js/' rel='bookmark' title='Permanent Link: Metatunnel 1k Demo: AS vs. JS'>Metatunnel 1k Demo: AS vs. JS</a></li><li><a href='http://blog.betabong.com/2008/09/01/speed-parsing-string-in-as3/' rel='bookmark' title='Permanent Link: Simplicity follows Performance – parsing Strings in Actionscript 3'>Simplicity follows Performance – parsing Strings in Actionscript 3</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Sometimes you want to pass an objects function as an argument and store that function (along its arguments) for later calling. Still you don&#8217;t want that storage to prevent its object (instance) from being destroyed (from being removed by the garbage collector). But storing a function does so, because it holds a reference to the function which belongs to an object.</p>
<p>It&#8217;s one of the biggest traps you can fall into when developing a larger project where many objects are created and destroyed and created and so on&#8230; geeks call that kind of thing memory leaks I think :)</p>
<p>So if you can avoid it: avoid it! If you can&#8217;t.. may be this little thing can help:<br />
<span id="more-115"></span></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">betabong</span>.<span style="color: #006600;">util</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">Dictionary</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> WeakMethodClosure <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> holder : Dictionary;
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> WeakMethodClosure<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">target</span> : <span style="color: #0066CC;">Object</span> , method : <span style="color: #000000; font-weight: bold;">Function</span> , <span style="color: #0066CC;">arguments</span> : <span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			holder = <span style="color: #000000; font-weight: bold;">new</span> Dictionary<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>;
			holder<span style="color: #66cc66;">&#91;</span> <span style="color: #0066CC;">target</span> <span style="color: #66cc66;">&#93;</span> = 	<span style="color: #000000; font-weight: bold;">new</span> MethodStorage<span style="color: #66cc66;">&#40;</span> method , <span style="color: #0066CC;">arguments</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> args : <span style="color: #0066CC;">Array</span>;
			<span style="color: #000000; font-weight: bold;">var</span> f : <span style="color: #000000; font-weight: bold;">Function</span>;
			<span style="color: #000000; font-weight: bold;">var</span> cache : MethodStorage;
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> obj : <span style="color: #66cc66;">*</span> <span style="color: #b1b100;">in</span> holder <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				cache = holder<span style="color: #66cc66;">&#91;</span>obj<span style="color: #66cc66;">&#93;</span> as MethodStorage;
				cache.<span style="color: #006600;">method</span>.<span style="color: #0066CC;">apply</span><span style="color: #66cc66;">&#40;</span> obj , cache.<span style="color: #0066CC;">arguments</span> <span style="color: #66cc66;">&#41;</span>;
				<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MethodStorage <span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> method : <span style="color: #000000; font-weight: bold;">Function</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">arguments</span> : <span style="color: #0066CC;">Array</span>;
	<span style="color: #000000; font-weight: bold;">function</span> MethodStorage<span style="color: #66cc66;">&#40;</span> method : <span style="color: #000000; font-weight: bold;">Function</span> , <span style="color: #0066CC;">arguments</span> : <span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">method</span> = method;
		<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">arguments</span> = <span style="color: #0066CC;">arguments</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This actually uses one of the two mechanisms Flash provides to store objects by weak reference: the magic Dictionary. So instead of storing the function, you actually store the method storage or call method:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> callAfterTime<span style="color: #66cc66;">&#40;</span> f : <span style="color: #000000; font-weight: bold;">Function</span> , a : <span style="color: #0066CC;">Array</span> , t : uint <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">storedfunction</span> = <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> WeakMethodClosure<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span> , f , a <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">call</span>;
<span style="color: #808080; font-style: italic;">// and later:</span>
storedfunction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>It might not be obvious where you&#8217;d wanna use this, but I guess if you&#8217;ve found your way to this blog entry, you might be able to find out if that&#8217;s of any value for you anyway ;)</p>
<p><a href="/showcase/WeakMethodClosure.as.zip">Download WeakMethodClosure.as (zip)</a></p>
<p>Just be aware that this solution might not be superduper save in all cases: the Garbage Collector is a wayward beast – there&#8217;s no guarantee it&#8217;ll clean your dictionary on time. Still, in my experience, it&#8217;s save enough. It&#8217;s comparable to adding a weak event listener..</p>
<p>And hey, if you&#8217;ve got any better idea, I&#8217;d love to hear!</p>
<p><strong>Update:</strong> Below a test application&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">betabong</span>.<span style="color: #006600;">util</span>.<span style="color: #006600;">WeakMethodClosure</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">TimerEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">Timer</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #0066CC;">getTimer</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> WeakClosureTest <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> closure : WeakMethodClosure;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> timer : Timer;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> WeakClosureTest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>			
			testWeakClosure<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testWeakClosure<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> tempObject : <span style="color: #0066CC;">Object</span> = <span style="color: #66cc66;">&#123;</span>
				test: testTrace
			<span style="color: #66cc66;">&#125;</span>
			closure = <span style="color: #000000; font-weight: bold;">new</span> WeakMethodClosure<span style="color: #66cc66;">&#40;</span> tempObject , tempObject.<span style="color: #006600;">test</span> <span style="color: #66cc66;">&#41;</span>;
			callWeakClosure<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			timer = <span style="color: #000000; font-weight: bold;">new</span> Timer<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">50</span> <span style="color: #66cc66;">&#41;</span>;
			timer.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TimerEvent.<span style="color: #006600;">TIMER</span> , callWeakClosure <span style="color: #66cc66;">&#41;</span>;
			timer.<span style="color: #0066CC;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> callWeakClosure<span style="color: #66cc66;">&#40;</span> event : TimerEvent = <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>closure.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;Closure failed&quot;</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testTrace<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Called test&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Outputs:</p>
<pre>
Called test
Called test
Closure failed
Closure failed
Closure failed
Closure failed
Closure failed
Closure failed
Closure failed
Closure failed
Closure failed
Closure failed
Closure failed
Closure failed
Closure failed
Closure failed
Closure failed
</pre>


<p>Related posts:<ol><li><a href='http://blog.betabong.com/2008/07/21/nested-inner-functions-can-be-evil/' rel='bookmark' title='Permanent Link: Nested inner functions can be evil'>Nested inner functions can be evil</a></li><li><a href='http://blog.betabong.com/2009/04/13/metatunnel-1k-demo-as-vs-js/' rel='bookmark' title='Permanent Link: Metatunnel 1k Demo: AS vs. JS'>Metatunnel 1k Demo: AS vs. JS</a></li><li><a href='http://blog.betabong.com/2008/09/01/speed-parsing-string-in-as3/' rel='bookmark' title='Permanent Link: Simplicity follows Performance – parsing Strings in Actionscript 3'>Simplicity follows Performance – parsing Strings in Actionscript 3</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.betabong.com/2008/09/26/weak-method-closure/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
