<?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: Weak Method Closure</title>
	<atom:link href="http://blog.betabong.com/2008/09/26/weak-method-closure/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.betabong.com/2008/09/26/weak-method-closure/</link>
	<description></description>
	<lastBuildDate>Tue, 20 Apr 2010 20:40:19 +0200</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Kirill</title>
		<link>http://blog.betabong.com/2008/09/26/weak-method-closure/comment-page-1/#comment-43</link>
		<dc:creator>Kirill</dc:creator>
		<pubDate>Fri, 29 May 2009 12:11:15 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=115#comment-43</guid>
		<description>BTW I&#039;m testing using FP10 which had a lot of the GC bugs fixed. All I said is moot in FP9, which as far as I&#039;m concerned has no garbage collection.</description>
		<content:encoded><![CDATA[<p>BTW I&#8217;m testing using FP10 which had a lot of the GC bugs fixed. All I said is moot in FP9, which as far as I&#8217;m concerned has no garbage collection.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kirill</title>
		<link>http://blog.betabong.com/2008/09/26/weak-method-closure/comment-page-1/#comment-42</link>
		<dc:creator>Kirill</dc:creator>
		<pubDate>Fri, 29 May 2009 12:09:14 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=115#comment-42</guid>
		<description>The first test is actually misleading, because it creates a dynamic object and has the WeakMethodClosure store a method actually defined in the Test as though it was part of this object. This doesn&#039;t work in AS3 because the method will always be owned by the test object due to its closure. The reason you saw it &quot;working&quot; is that you didn&#039;t store the dynamic object anywhere and the weak dictionary allowed it to be GCed, but the real owner of the method didn&#039;t get GCed since it has references to it.

The 2nd test is more correct, but it stores a reference to WeakMethodClosure in the main object rather than in TestObject. This creates an outside reference and stops TestObject and WeakMethodClosure from getting Gced. For this to work the &#039;closure&#039; instance variable has to be moved to TestObject. But there&#039;s another reference via the Timer event listener, which isn&#039;t weak in the test so that will also prevent garbage collection. That listener has to be weak for it to not count as a reference into the circuit.

One other thing is this has to be tested inside the profiler and not in the debugger if you guys are using FlexBuilder. Because the debugger has somekind of a bug that prevents garbage collector from running as opposed to the profiler.</description>
		<content:encoded><![CDATA[<p>The first test is actually misleading, because it creates a dynamic object and has the WeakMethodClosure store a method actually defined in the Test as though it was part of this object. This doesn&#8217;t work in AS3 because the method will always be owned by the test object due to its closure. The reason you saw it &#8220;working&#8221; is that you didn&#8217;t store the dynamic object anywhere and the weak dictionary allowed it to be GCed, but the real owner of the method didn&#8217;t get GCed since it has references to it.</p>
<p>The 2nd test is more correct, but it stores a reference to WeakMethodClosure in the main object rather than in TestObject. This creates an outside reference and stops TestObject and WeakMethodClosure from getting Gced. For this to work the &#8216;closure&#8217; instance variable has to be moved to TestObject. But there&#8217;s another reference via the Timer event listener, which isn&#8217;t weak in the test so that will also prevent garbage collection. That listener has to be weak for it to not count as a reference into the circuit.</p>
<p>One other thing is this has to be tested inside the profiler and not in the debugger if you guys are using FlexBuilder. Because the debugger has somekind of a bug that prevents garbage collector from running as opposed to the profiler.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kirill</title>
		<link>http://blog.betabong.com/2008/09/26/weak-method-closure/comment-page-1/#comment-41</link>
		<dc:creator>Kirill</dc:creator>
		<pubDate>Fri, 29 May 2009 11:43:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=115#comment-41</guid>
		<description>Great to see follow-ups on this. This is indeed very frustrating. But... It is possible to do as I&#039;ve learned. When storing references to methods what has to happen for garbage collection to act right is the objects involved must form a closed circuit. If there&#039;s ANY reference to one of the objects in the circuit from outside then none of them will be garbage collected. Note that this is actually desired behavior for only one of the objects in the circuit, which is the user object. So imagine we have the user object and the WeakMethodClosure which stores a reference to its method. What has to happen is the user object must store a reference to the WeakClosureMethod  object. When the user object has references to it from outside the circuit then neither it or the WeakMethodClosure get GCed (which is correct), when no outside reference exists then they both get garbage collected (desired behavior). If there isn&#039;t a circuit and the user object doesn&#039;t have a reference to the WeakMethodClosure then WeakMethodClosure object will get GCed even when the user has references to it and doesn&#039;t get GCed. If there&#039;s any reference to WeakMethodClosure other than from user object or any other object in the circuit then nothing will get GCed.

The problem with creating a utility like WeakMethodClosure is forming that reference from the user to it. WeakMethodClosure can&#039;t do this automatically without placing some really heavy restrictions on the user. Like requiring it to be dynamic, or subclass of EventDispatcher (so that it can add a dummy event listener to itself), or an interface which requires the hassle of implementing a way to store WeakMethodClosure in the user. One more or less sensible way to implement this utility is adding a 3rd object to store WeakMethodClosure. So that the user has to explicitly define a variable and assign an instance of this manager to it and then instead of doing new WeakMethodClosure( myMethod ), it would do weakMethods.createClosure( myMethod ). Note that the user, manager, and WeakMethodClosure form a circuit so it&#039;s fine. It&#039;s an ugly interface but it works and there&#039;s considerably less magic in it than in other ways of attempting to do this.</description>
		<content:encoded><![CDATA[<p>Great to see follow-ups on this. This is indeed very frustrating. But&#8230; It is possible to do as I&#8217;ve learned. When storing references to methods what has to happen for garbage collection to act right is the objects involved must form a closed circuit. If there&#8217;s ANY reference to one of the objects in the circuit from outside then none of them will be garbage collected. Note that this is actually desired behavior for only one of the objects in the circuit, which is the user object. So imagine we have the user object and the WeakMethodClosure which stores a reference to its method. What has to happen is the user object must store a reference to the WeakClosureMethod  object. When the user object has references to it from outside the circuit then neither it or the WeakMethodClosure get GCed (which is correct), when no outside reference exists then they both get garbage collected (desired behavior). If there isn&#8217;t a circuit and the user object doesn&#8217;t have a reference to the WeakMethodClosure then WeakMethodClosure object will get GCed even when the user has references to it and doesn&#8217;t get GCed. If there&#8217;s any reference to WeakMethodClosure other than from user object or any other object in the circuit then nothing will get GCed.</p>
<p>The problem with creating a utility like WeakMethodClosure is forming that reference from the user to it. WeakMethodClosure can&#8217;t do this automatically without placing some really heavy restrictions on the user. Like requiring it to be dynamic, or subclass of EventDispatcher (so that it can add a dummy event listener to itself), or an interface which requires the hassle of implementing a way to store WeakMethodClosure in the user. One more or less sensible way to implement this utility is adding a 3rd object to store WeakMethodClosure. So that the user has to explicitly define a variable and assign an instance of this manager to it and then instead of doing new WeakMethodClosure( myMethod ), it would do weakMethods.createClosure( myMethod ). Note that the user, manager, and WeakMethodClosure form a circuit so it&#8217;s fine. It&#8217;s an ugly interface but it works and there&#8217;s considerably less magic in it than in other ways of attempting to do this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: betabong</title>
		<link>http://blog.betabong.com/2008/09/26/weak-method-closure/comment-page-1/#comment-38</link>
		<dc:creator>betabong</dc:creator>
		<pubDate>Mon, 20 Apr 2009 15:32:04 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=115#comment-38</guid>
		<description>@Thijs: Good point. Didn&#039;t think about that, but it makes absolutely sense. One more reason why Adobe should allow us to somehow access the MethodClosure. Apart from that I conclude that it&#039;s still best practice to just have clean coding instead of using semi-functional hacks like those (mine) above.</description>
		<content:encoded><![CDATA[<p>@Thijs: Good point. Didn&#8217;t think about that, but it makes absolutely sense. One more reason why Adobe should allow us to somehow access the MethodClosure. Apart from that I conclude that it&#8217;s still best practice to just have clean coding instead of using semi-functional hacks like those (mine) above.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thijs</title>
		<link>http://blog.betabong.com/2008/09/26/weak-method-closure/comment-page-1/#comment-37</link>
		<dc:creator>Thijs</dc:creator>
		<pubDate>Sun, 19 Apr 2009 12:50:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=115#comment-37</guid>
		<description>&quot;Note that there is a known bug with Dictionary that prevents it from operating correctly with references to methods. It seems that Dictionary does not resolve the method reference properly, and uses the closure object (ie. the &quot;behind the scenes&quot; object that facilitates method closure by maintaining a reference back to the method and its scope) instead of the function as the key. This causes two problems: the reference is immediately available for collection in a weak Dictionary (because while the method is still referenced, the closure object is not), and it can create duplicate entries if you add the same method twice. This can cause some big problems for things like doLater queues. &quot;

http://www.gskinner.com/blog/archives/2006/07/as3_dictionary.html</description>
		<content:encoded><![CDATA[<p>&#8220;Note that there is a known bug with Dictionary that prevents it from operating correctly with references to methods. It seems that Dictionary does not resolve the method reference properly, and uses the closure object (ie. the &#8220;behind the scenes&#8221; object that facilitates method closure by maintaining a reference back to the method and its scope) instead of the function as the key. This causes two problems: the reference is immediately available for collection in a weak Dictionary (because while the method is still referenced, the closure object is not), and it can create duplicate entries if you add the same method twice. This can cause some big problems for things like doLater queues. &#8221;</p>
<p><a href="http://www.gskinner.com/blog/archives/2006/07/as3_dictionary.html" rel="nofollow">http://www.gskinner.com/blog/archives/2006/07/as3_dictionary.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thijs</title>
		<link>http://blog.betabong.com/2008/09/26/weak-method-closure/comment-page-1/#comment-36</link>
		<dc:creator>Thijs</dc:creator>
		<pubDate>Wed, 15 Apr 2009 12:02:03 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=115#comment-36</guid>
		<description>My solution is not working either :&#039;-(

I am trying to create a better solution for creating weak references to a method. But I guess it&#039;s not possible.

For some reasons it&#039;s not possible to use a method as a key in a Dictionary with weak-keys (they are deleted immediatly, even when the method still exists), so maybe it&#039;s a bug.</description>
		<content:encoded><![CDATA[<p>My solution is not working either :&#8217;-(</p>
<p>I am trying to create a better solution for creating weak references to a method. But I guess it&#8217;s not possible.</p>
<p>For some reasons it&#8217;s not possible to use a method as a key in a Dictionary with weak-keys (they are deleted immediatly, even when the method still exists), so maybe it&#8217;s a bug.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: betabong</title>
		<link>http://blog.betabong.com/2008/09/26/weak-method-closure/comment-page-1/#comment-35</link>
		<dc:creator>betabong</dc:creator>
		<pubDate>Wed, 15 Apr 2009 11:17:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=115#comment-35</guid>
		<description>To Thijs:

Useless is a bit harsh for my poor class ;) It&#039;s just useless as soon as you use an instance function. Though you&#039;re right: it *usually* is totally useless :)

For what you want (and what I probably want too) we just have to make the closure a little bit weaker:

&lt;pre lang=&quot;actionscript&quot;&gt;
package com.betabong.util
{
	import flash.utils.Dictionary;
	
	public class WeakerMethodClosure
	{
		private var holder : Dictionary;
		
		function WeakerMethodClosure( target : Object , method : Function , arguments : Array = null ) : void {
			var fholder : Dictionary = new Dictionary( true );
			fholder[ method ] = arguments;
			holder = new Dictionary( true );
			holder[ target ] = 	fholder;
		}
		
		public function call() : Boolean {
			var args : Array;
			var f : Function;
			var cache : Dictionary;
			for ( var obj : * in holder ) {
				cache = holder[obj] as Dictionary;
				for ( var fnc : * in cache ) {
					f = fnc as Function;
					args = cache[ fnc ] as Array;
					f.apply( obj , args );
					return true;
				}
			}
			return false;
		}
	}
}
&lt;/pre&gt;

testing with:

&lt;pre lang=&quot;actionscript&quot;&gt;
package {
	import com.betabong.util.WeakerMethodClosure;
	
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import test.TestTrace;

	public class ActionscriptTests extends Sprite
	{
		private var holder : WeakerMethodClosure;
		
		public function ActionscriptTests()
		{
			init();
		}

		public function init() : void {
			var o:TestTrace = new TestTrace();
			holder = new WeakerMethodClosure( o , o.test );

			// let&#039;s call it once to make sure it works
			holder.call();

			var timer : Timer = new Timer( 1000 , 10 );
			timer.addEventListener(TimerEvent.TIMER , call );
			timer.start();
		}
		
		public function call( e:* ) : void {
			trace( &quot;Calling holder&quot; );
			holder.call();
		}
	}
}
&lt;/pre&gt;

and:

&lt;pre lang=&quot;actionscript&quot;&gt;
package test
{
	public class TestTrace
	{
		static public var globalindex : uint = 0;
		private var localindex : uint = 0;
		private var callcounter : uint = 0;
		
		public function TestTrace()
		{
			localindex = ++globalindex;
			trace( toString() + &quot; created&quot; );
		}
		
		public function test() : void {
			trace( toString() + &quot; called &quot; + (++callcounter) + &quot; times&quot; );
		}
		
		public function toString() : String {
			return &quot;TestTrace &quot; + localindex;
		}
		

	}
}
&lt;/pre&gt;

outputs:

&lt;pre lang=&quot;text&quot;&gt;
TestTrace 1 created
TestTrace 1 called 1 times
Calling holder
Calling holder
Calling holder
Calling holder
&lt;/pre&gt;

Is this more useful? Probably. A very little bit. Under normal situations we hopefully don&#039;t have to use such weird constructs.</description>
		<content:encoded><![CDATA[<p>To Thijs:</p>
<p>Useless is a bit harsh for my poor class ;) It&#8217;s just useless as soon as you use an instance function. Though you&#8217;re right: it *usually* is totally useless :)</p>
<p>For what you want (and what I probably want too) we just have to make the closure a little bit weaker:</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> WeakerMethodClosure
	<span style="color: #66cc66;">&#123;</span>
		<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> WeakerMethodClosure<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>
			<span style="color: #000000; font-weight: bold;">var</span> fholder : Dictionary = <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>;
			fholder<span style="color: #66cc66;">&#91;</span> method <span style="color: #66cc66;">&#93;</span> = <span style="color: #0066CC;">arguments</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> = 	fholder;
		<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 : Dictionary;
			<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 Dictionary;
				<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> fnc : <span style="color: #66cc66;">*</span> <span style="color: #b1b100;">in</span> cache <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
					f = fnc as <span style="color: #000000; font-weight: bold;">Function</span>;
					args = cache<span style="color: #66cc66;">&#91;</span> fnc <span style="color: #66cc66;">&#93;</span> as <span style="color: #0066CC;">Array</span>;
					f.<span style="color: #0066CC;">apply</span><span style="color: #66cc66;">&#40;</span> obj , args <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: #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></pre></div></div>

<p>testing with:</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;">WeakerMethodClosure</span>;
&nbsp;
	<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>;
&nbsp;
	<span style="color: #0066CC;">import</span> test.<span style="color: #006600;">TestTrace</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ActionscriptTests <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> holder : WeakerMethodClosure;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ActionscriptTests<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			init<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> init<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> o:TestTrace = <span style="color: #000000; font-weight: bold;">new</span> TestTrace<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			holder = <span style="color: #000000; font-weight: bold;">new</span> WeakerMethodClosure<span style="color: #66cc66;">&#40;</span> o , o.<span style="color: #006600;">test</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// let's call it once to make sure it works</span>
			holder.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> timer : Timer = <span style="color: #000000; font-weight: bold;">new</span> Timer<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">1000</span> , <span style="color: #cc66cc;">10</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> , <span style="color: #0066CC;">call</span> <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> <span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">e</span>:<span style="color: #66cc66;">*</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;Calling holder&quot;</span> <span style="color: #66cc66;">&#41;</span>;
			holder.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>and:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package test
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestTrace
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> globalindex : uint = <span style="color: #cc66cc;">0</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> localindex : uint = <span style="color: #cc66cc;">0</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> callcounter : uint = <span style="color: #cc66cc;">0</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: #66cc66;">&#123;</span>
			localindex = ++globalindex;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; created&quot;</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> test<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: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; called &quot;</span> + <span style="color: #66cc66;">&#40;</span>++callcounter<span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; times&quot;</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;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;TestTrace &quot;</span> + localindex;
		<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>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">TestTrace 1 created
TestTrace 1 called 1 times
Calling holder
Calling holder
Calling holder
Calling holder</pre></div></div>

<p>Is this more useful? Probably. A very little bit. Under normal situations we hopefully don&#8217;t have to use such weird constructs.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thijs</title>
		<link>http://blog.betabong.com/2008/09/26/weak-method-closure/comment-page-1/#comment-34</link>
		<dc:creator>Thijs</dc:creator>
		<pubDate>Wed, 15 Apr 2009 10:58:50 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=115#comment-34</guid>
		<description>I also found out a solution:

 - Store the object inside the MethodStorage 
 - use the MethodStorage as key
 - use a dummy value (like true, or a timestamp) as value</description>
		<content:encoded><![CDATA[<p>I also found out a solution:</p>
<p> &#8211; Store the object inside the MethodStorage<br />
 &#8211; use the MethodStorage as key<br />
 &#8211; use a dummy value (like true, or a timestamp) as value</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thijs</title>
		<link>http://blog.betabong.com/2008/09/26/weak-method-closure/comment-page-1/#comment-33</link>
		<dc:creator>Thijs</dc:creator>
		<pubDate>Wed, 15 Apr 2009 10:27:37 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=115#comment-33</guid>
		<description>The weakreference only works for anonymous objects. When you use typed objects, the object is still kept in memory. I think this is caused by method inside the MethodStorage object.

Try this example:
&lt;pre lang=&quot;actionscript&quot;&gt;
package  
{
	import com.betabong.util.WeakMethodClosure;
	
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;	

	public class NotificationCenterExample extends Sprite 
	{
		private var closure : WeakMethodClosure;
		private var timer : Timer;
 
		public function NotificationCenterExample()
		{			
			testWeakClosure();
		}
 
		public function testWeakClosure() : void {
			var tempObject:TestObject = new TestObject();
			closure = new WeakMethodClosure( tempObject , tempObject.test );
			callWeakClosure();
 
			timer = new Timer( 50 );
			timer.addEventListener(TimerEvent.TIMER , callWeakClosure );
			timer.start();
		}
 
		public function callWeakClosure( event : TimerEvent = null ) : void {
			if ( !closure.call() ) {
				trace( &quot;Closure failed&quot; );
			}
		}
	}
}

class TestObject
{
	public function test():void
	{
		trace(&quot;Called test&quot;);
	}
}
&lt;/pre&gt;

Outputs:
&lt;pre lang=&quot;text&quot;&gt;
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
&lt;/pre&gt;

So I guess your class is useless</description>
		<content:encoded><![CDATA[<p>The weakreference only works for anonymous objects. When you use typed objects, the object is still kept in memory. I think this is caused by method inside the MethodStorage object.</p>
<p>Try this example:</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>;
&nbsp;
	<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>;	
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NotificationCenterExample <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> NotificationCenterExample<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:TestObject = <span style="color: #000000; font-weight: bold;">new</span> TestObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</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>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> TestObject
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> test<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>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Outputs:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test
Called test</pre></div></div>

<p>So I guess your class is useless</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: betabong</title>
		<link>http://blog.betabong.com/2008/09/26/weak-method-closure/comment-page-1/#comment-19</link>
		<dc:creator>betabong</dc:creator>
		<pubDate>Sun, 11 Jan 2009 12:12:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=115#comment-19</guid>
		<description>Kirill, I&#039;ve added a test application to the post. As you can see the garbage collection gets rid of the temporary object, though has to be run once (that&#039;s why the method can be called by the TimerEvent although it&#039;s ready to be garbaged already). So to be absolutely sure that this works as intended, we&#039;d need to force garbage collection before each call (wasn&#039;t there a command for that? hmm..)

Its main purpose is though to avoid accidental memory leaks, and for this it has it&#039;s uses. There is btw a class flash.events.WeakMethodClosure that&#039;s used internally – I&#039;m not sure what it does exactly though (no code available..).

You certainly have more knowledge of the garbage collection process, so if you can shed more light on this, it would be cool. I&#039;d also love to hear from the Flash Player team what exactly is going on with those weak references, so we&#039;re not so much moving in the dark.</description>
		<content:encoded><![CDATA[<p>Kirill, I&#8217;ve added a test application to the post. As you can see the garbage collection gets rid of the temporary object, though has to be run once (that&#8217;s why the method can be called by the TimerEvent although it&#8217;s ready to be garbaged already). So to be absolutely sure that this works as intended, we&#8217;d need to force garbage collection before each call (wasn&#8217;t there a command for that? hmm..)</p>
<p>Its main purpose is though to avoid accidental memory leaks, and for this it has it&#8217;s uses. There is btw a class flash.events.WeakMethodClosure that&#8217;s used internally – I&#8217;m not sure what it does exactly though (no code available..).</p>
<p>You certainly have more knowledge of the garbage collection process, so if you can shed more light on this, it would be cool. I&#8217;d also love to hear from the Flash Player team what exactly is going on with those weak references, so we&#8217;re not so much moving in the dark.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kirill</title>
		<link>http://blog.betabong.com/2008/09/26/weak-method-closure/comment-page-1/#comment-16</link>
		<dc:creator>Kirill</dc:creator>
		<pubDate>Fri, 09 Jan 2009 13:12:43 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=115#comment-16</guid>
		<description>I&#039;m not sure how gc safe this is. A weak dictionary doesn&#039;t increase the reference count of an object used as a key in it, but does it also not increase the reference count of any objects stored IN the objects that are in it? You operate under the assumption that it does. But I don&#039;t think that may be the case. What I mean is that when you do holder[ target ] = ... target doesn&#039;t have its reference count increased allowing it to be gced later. BUT you store a MethodStorage object, which has references to the method and the arguments array. This will cause the reference counts of the method and the array to increase. The method by virtue of its method closure will hold a reference back to the target. Since this doesn&#039;t seem to be a circular reference scenario, which I think fp gc catches, this method&#039;s reference back to the target will prevent it from being gced since the method&#039;s reference count has increased. As well as this, because the arguments array has references back to the objects stored in it those objects will not be gced either.

I&#039;m not saying code code doesn&#039;t work period, I may in fact be wrong. But from looking at it I&#039;m seeing a flaw in it. I&#039;d be interested in seeing the test code you have for this and trying it out.</description>
		<content:encoded><![CDATA[<p>I&#8217;m not sure how gc safe this is. A weak dictionary doesn&#8217;t increase the reference count of an object used as a key in it, but does it also not increase the reference count of any objects stored IN the objects that are in it? You operate under the assumption that it does. But I don&#8217;t think that may be the case. What I mean is that when you do holder[ target ] = &#8230; target doesn&#8217;t have its reference count increased allowing it to be gced later. BUT you store a MethodStorage object, which has references to the method and the arguments array. This will cause the reference counts of the method and the array to increase. The method by virtue of its method closure will hold a reference back to the target. Since this doesn&#8217;t seem to be a circular reference scenario, which I think fp gc catches, this method&#8217;s reference back to the target will prevent it from being gced since the method&#8217;s reference count has increased. As well as this, because the arguments array has references back to the objects stored in it those objects will not be gced either.</p>
<p>I&#8217;m not saying code code doesn&#8217;t work period, I may in fact be wrong. But from looking at it I&#8217;m seeing a flaw in it. I&#8217;d be interested in seeing the test code you have for this and trying it out.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aidan</title>
		<link>http://blog.betabong.com/2008/09/26/weak-method-closure/comment-page-1/#comment-8</link>
		<dc:creator>Aidan</dc:creator>
		<pubDate>Thu, 09 Oct 2008 03:23:03 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=115#comment-8</guid>
		<description>Fantastic. Like you say, it&#039;s not completely obvious to me _exactly_ where I&#039;ll use this in my large, complex as3 application, but I&#039;m very sure it will be very handy. Unfortunately, it&#039;s not just functions that are obstructing garbage collection for me.

Reassuring to know that other people are having similar memory hurdles. Cheers!</description>
		<content:encoded><![CDATA[<p>Fantastic. Like you say, it&#8217;s not completely obvious to me _exactly_ where I&#8217;ll use this in my large, complex as3 application, but I&#8217;m very sure it will be very handy. Unfortunately, it&#8217;s not just functions that are obstructing garbage collection for me.</p>
<p>Reassuring to know that other people are having similar memory hurdles. Cheers!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
