<?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>Reign Drops Fall... &#187; internet explorer</title>
	<atom:link href="http://www.reigndropsfall.net/tag/internet-explorer/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.reigndropsfall.net</link>
	<description>Ramblings from a hacker in Iowa</description>
	<lastBuildDate>Wed, 09 May 2012 19:48:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Internet Explorer Event Handler Leaks</title>
		<link>http://www.reigndropsfall.net/2011/01/05/internet-explorer-event-handler-leaks/</link>
		<comments>http://www.reigndropsfall.net/2011/01/05/internet-explorer-event-handler-leaks/#comments</comments>
		<pubDate>Wed, 05 Jan 2011 20:30:26 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[internet explorer]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.reigndropsfall.net/?p=13953</guid>
		<description><![CDATA[If you&#8217;ve been developing for the open web for long, you&#8217;ll know that Internet Explorer is the bane of any web developer&#8217;s existence. The number of CSS bugs are enough to give any web developer a headache, but then add to that JScript&#8217;s deviations from the ECMAScript 3 spec and you have yourself a pain [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve been developing for the open web for long, you&#8217;ll know that Internet Explorer is the bane of any web developer&#8217;s existence.  The number of <a href="http://www.positioniseverything.net/explorer.html">CSS bugs</a> are enough to give any web developer a headache, but then add to that <a href="http://wiki.ecmascript.org/lib/exe/fetch.php?id=resources%3Aresources&#038;cache=cache&#038;media=resources:jscriptdeviationsfromes3.pdf">JScript&#8217;s deviations from the ECMAScript 3 spec</a> and you have yourself a pain in the neck as well.  Grab your favorite pain reliever, because there is another nuisance: memory leaks.</p>
<h2>Leaks, Circular References, and Host Objects</h2>
<p>There are two types of leaks in web development: single-page and cross-page.  A single-page leak happens when an object is not collected by the garbage collector (GC) while the page is running, but is cleaned up when the page is unloaded.  The cause of single-page leaks is usually an object being referenced unexpectedly.  A cross-page leak happens when an object is not collected by the GC when the page is unloaded.  We will be focusing on cross-page leaks in this blog post; from this point on, &#8220;leak&#8221; will refer to cross-page leaks.</p>
<p>The cause of cross-page leaks is usually circular references that trip up the GC.  A circular reference is formed when object A references object B which, in turn, references object A.  This seems to be pretty simple to avoid, but you can add more objects into the mix and still have a circular reference: <code>A -> B -> C -> D -> A</code>.  In garbage collected languages, such as ECMAScript (which includes JavaScript and JScript), circular references are (or should be) handled and cleaned up properly.  In fact, most implementations of ECMAScript do a good job of cleaning up circular references between native objects.  However, ECMAScript 3 defined something called &#8220;host objects&#8221; that don&#8217;t have to follow the rules that natvie objects do (see <a href="http://perfectionkills.com/whats-wrong-with-extending-the-dom/#host_objects_have_no_rules">What&#8217;s Wrong With Extending the DOM</a> by Juriy Zaytsev for a run-down of host object wackiness).  Circular references between host objects and native objects are what trips Internet Explorer up.  In fact, all versions of Internet Explorer &#8211; except patched versions of 6 and version 7 &#8211; leak when you leave a page that contains code that forms circular references between host objects and native objects (yes, that includes version 8).</p>
<p>Why do I mention host objects and why can&#8217;t we avoid them all together?  <strong>Internet Explorer implements DOM nodes and <code>ActiveXObject</code>s as host objects that wrap COM+ objects</strong> (the exception to the rule is version 8 where only ActiveXObjects are host objects; you can <a href="http://www.reigndropsfall.net/demos/ie_leak/leak_activexobject.html">run this test</a> to confirm the <code>ActiveXObject</code> leak).  This wouldn&#8217;t be so bad, except the interaction between the garbage collection in JScript and the reference counting that COM+ objects use cause problems.  When a native object (NO) is referenced, it is <a href="http://blogs.msdn.com/b/ericlippert/archive/2003/09/17/53038.aspx">put on the &#8220;scavenger&#8221; list</a> until it is no longer referenced; when a host object (HO) is referenced, the COM+ object&#8217;s reference count is increased and will only be destroyed when the reference count reaches 0.  When a NO and HO are in a circular reference, the HO&#8217;s COM+ object&#8217;s reference count will be decreased when the NO is garbage collected or stops referencing the HO; the NO will be taken off the &#8220;scavenger&#8221; list when nothing references it &#8211; including COM+ objects.  This means any DOM node or <code>ActiveXObject</code> that references native objects has the potential to leak when the page is unloaded.  But all is not lost!  The pattern is easy to identify and there is a fail-safe way to keep leaks from happening.</p>
<h2>Identifying the Pattern</h2>
<p>The basic pattern is this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">DOM_Node.objectRef -&gt;
    Object -&gt;
    Object.nodeRef -&gt;
    DOM_Node</pre></div></div>

<p>This translates to the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;someElement&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
elem.<span style="color: #660066;">someObject</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
obj.<span style="color: #660066;">someElement</span> <span style="color: #339933;">=</span> elem<span style="color: #339933;">;</span></pre></div></div>

<p>This is pretty easy to spot (and is a reminder of why not to use expando properties that are non-primitive), but remember that you can add multiple objects into the mix and still have a circular reference:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;someElement&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    obj1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> obj2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
elem.<span style="color: #660066;">someObject</span> <span style="color: #339933;">=</span> obj1<span style="color: #339933;">;</span>
obj1.<span style="color: #660066;">someObject</span> <span style="color: #339933;">=</span> obj2<span style="color: #339933;">;</span>
obj2.<span style="color: #660066;">someElement</span> <span style="color: #339933;">=</span> elem<span style="color: #339933;">;</span></pre></div></div>

<p>That seems easy enough, but there&#8217;s a much more elusive version of this pattern (adapted from <a href="http://www.jibbering.com/faq/notes/closures/#clMem">the comp.lang.javascript FAQ on closures</a>):</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">DOM_Node.onevent -&gt;
    inner_function_object -&gt;
    inner_function_object.scope_chain -&gt;
    outer_activation_object -&gt;
    outer_activation_object.nodeRef -&gt;
    DOM_Node</pre></div></div>

<p>This translates into the following (note the surrounding closure):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    elem.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;onclick&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        elem.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I&#8217;m not going to explain closures (for a good run-down, see the aforementioned FAQ), but the basic gist of what is happening here is a circular reference is formed through the <a href="http://bclary.com/2004/11/07/#a-10.1.6">activation object</a> of the outer function when accessed through the <a href="http://bclary.com/2004/11/07/#a-10.1.4">scope chain</a> of the event handler.  In pseudo-code, the ES3 internals would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">outer_activation_object.<span style="color: #660066;">elem</span> <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// The scope chain is formed by prepending inner_func's activation</span>
<span style="color: #006600; font-style: italic;">// object to a copy of inner_func's [[Scope]], which is a copy of</span>
<span style="color: #006600; font-style: italic;">// outer_func's scope chain.</span>
inner_func.<span style="color: #660066;">scope_chain</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
    inner_activation_object<span style="color: #339933;">,</span>
    outer_activation_object<span style="color: #339933;">,</span>
    global_object
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
elem.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> anonfunc<span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, the reference chain looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">elem.onclick -&gt;
    inner_func -&gt;
    inner_func.scope_chain -&gt;
    outer_activation_object -&gt;
    outer_activation_object.elem -&gt;
    elem</pre></div></div>

<p>Visiting one or two pages that leak won&#8217;t be too bad (unless they&#8217;re using an inordinate amount of event handlers).  However, if you visit several pages that have circular references on them you will notice IE consuming more and more memory.  I&#8217;ve put together a <a href="http://www.reigndropsfall.net/demos/ie_leak/leak.html">page that will leak in IE6</a> and refresh itself every 2 seconds to demonstrate the leak.  You can use the system monitor to monitor overall system memory usage or a tool like <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx">Process Explorer</a> to monitor the memory usage of individual processes.  This example will be our starting point and we&#8217;ll look at a couple ways to keep a page like this from leaking.  All of the examples in this article can also be downloaded in a <a href="http://www.reigndropsfall.net/demos/ie_leak.tar.bz2">tarball</a> or a <a href="http://www.reigndropsfall.net/demos/ie_leak.zip">zip file</a>.</p>
<p>If you look at the source of this leaky page, you will notice I include <a href="http://www.reigndropsfall.net/demos/ie_leak/helper.js"><code>helper.js</code></a>.  As the name suggests, this file has some helper functions:</p>
<ul>
<li><code>isHostType</code>: A function to safely check for the presence of properties on a host object.  If you&#8217;re not familiar with the reasoning behind this function or the gotchas of host objects, I suggest you read Peter Michaux&#8217;s article on <a href="http://peter.michaux.ca/articles/feature-detection-state-of-the-art-browser-scripting">feature detection</a>.  If you&#8217;re still looking at <code>navigator.userAgent</code> to know if you should use <code>addEventListener</code>, <code>attachEvent</code>, or <code>elem.onevent = function(){}</code> (DOM0), you need to read this article.  The version of this function I&#8217;m using is copied from <a href="http://github.com/phiggins42/has.js">has.js</a>.</li>
<li><code>_listen</code> and <code>_stopListening</code>: Wrapper functions around the event registration mechanism supported by the current browser.</li>
<li><code>normalizeEventName</code>: A function that makes sure the event name passed in is correct for the current browser.</li>
</ul>
<p>As we look at the different ways to break the leak pattern, I will be modifying the <code>listen</code> and <code>stopListening</code> functions within source of the page; I won&#8217;t have to modify anything in <code>helpers.js</code>.  Now that we have all of that out of the way, let&#8217;s start exploring ways to prevent event handler leaks.</p>
<h2>Fixing It, the Microsoft Way</h2>
<p>Microsoft is well aware of this problem; in fact, they have an <a href="http://msdn.microsoft.com/en-us/library/bb250448(VS.85).aspx">MSDN article on leak patterns in IE</a>, <a href="http://siteexperts.spaces.live.com/Blog/cns!1pNcL8JwTfkkjv4gg6LkVCpw!338.entry">an article from Scott Isaacs</a>, and a <a href="http://support.microsoft.com/default.aspx?scid=KB;EN-US;830555">Knowledge Base article</a> as well.  Let&#8217;s take a look at the ways they recommend to fix the problem.</p>
<h3>Detach on Unload</h3>
<p>The first way that Microsoft recommends is to call <code>elem.detachEvent(eventName, handler)</code> in an unload handler.  The example in the MSDN article mentioned earlier recommends storing the handler in an expando property of the node, however I won&#8217;t show that example here because storing things in an expando property of a node is a no-no in IE for a few reasons:</p>
<ul>
<li>Changing an expando causes the node to re-render</li>
<li>Expandos with primitive values are cloned when you clone a node</li>
<li>Expandos that store non-primitive values can contribute to circular references</li>
</ul>
<p>The third reason is the one that should make you wary of their solution; as we saw earlier, expandos are an easy way to make circular references and their solution is no exception.  Suppose the node with the expando on it is destroyed via <code>.innerHTML</code>: unless we keep a reference to that node around, we&#8217;ve lost it and cannot detach the handler and we now have a leak.</p>
<p>A slightly better solution that runs in the same vein as Microsoft&#8217;s would be to register an unload handler, which forms a closure around the element, for each event registered:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> listen<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> e <span style="color: #339933;">=</span> normalizeEventName<span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    _listen<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> e<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// unload every attached event</span>
    window.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;onunload&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        obj.<span style="color: #660066;">detachEvent</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This breaks the circular reference manually by removing the reference to <code>handler</code> from the element.  You can see for yourself in <a href="http://www.reigndropsfall.net/demos/ie_leak/no_leak_ms_unload_1.html">this example</a>.  Although this works, it could potentially be registering hundreds, if not thousands, of unload handlers (depending on its use).  A better solution to this would be to use one unload handler with a cache of the arguments used to register each handler:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>global<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> _evtData <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> _emptyObject <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> _nextId <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
        unloadAttached <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> attachUnload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>isHostType<span style="color: #009900;">&#40;</span>document<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;attachEvent&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// one onunload handler to rule them all</span>
            global.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;onunload&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> _evtData<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>i <span style="color: #000066; font-weight: bold;">in</span> _emptyObject<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        stopListening<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        unloadAttached <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> listen<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> _nextId<span style="color: #339933;">++,</span>
            e <span style="color: #339933;">=</span> normalizeEventName<span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        _listen<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> e<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        _evtData<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>obj<span style="color: #339933;">,</span> e<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>unloadAttached<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            attachUnload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> id<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #003366; font-weight: bold;">function</span> stopListening<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>id <span style="color: #000066; font-weight: bold;">in</span> _evtData<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> _evtData<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            _stopListening.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">delete</span> _evtData<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    global.<span style="color: #660066;">listen</span> <span style="color: #339933;">=</span> listen<span style="color: #339933;">;</span>
    global.<span style="color: #660066;">stopListening</span> <span style="color: #339933;">=</span> stopListening<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The API changes slightly: <code>listen</code> now returns the ID of the cached arguments, and <code>stopListening</code> accepts that ID.  It seems that this is a simple solution to a big problem!</p>
<p>This simple solution, however, introduces a new problem: adding an unload handler breaks the &#8220;back/forward&#8221; cache (or, bfcache) in <a href="https://developer.mozilla.org/En/Using_Firefox_1.5_caching">modern</a> <a href="http://webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/">browsers</a>.  Although we added a check for <code>attachEvent</code> in our <code>attachUnload</code> function to weed out Firefox, Safari, and Chrome, this would break bfcache in Opera (since Opera has <code>attachEvent</code>); it also could match IE10+ where bfcache might be implemented.  Since there&#8217;s no reliable way to detect if a browser has bfcache, it&#8217;s best to avoid possibly breaking it.</p>
<h3>Define Handlers in Another Scope</h3>
<p>Another solution Microsoft suggests is to define all event handlers in a separate scope so the element isn&#8217;t in the handlers&#8217; scope chain; the code shown in their example uses the global scope, but having all of your event handlers in the global scope is impractical for larger applications.  For this solution, we&#8217;ll revert to <code>listen</code> and <code>stopListening</code> from the original leaking example and <a href="http://www.reigndropsfall.net/demos/ie_leak/no_leak_ms_scope.html">modify the loop</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span>i <span style="color: #339933;">&lt;</span> l<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> hookupOnClick <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">function</span> handler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            listen<span style="color: #009900;">&#40;</span>elem<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;onclick&quot;</span><span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">return</span> handler<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> e <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> handler <span style="color: #339933;">=</span> hookupOnClick<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #006600; font-style: italic;">// use stopListening(e, &quot;onclick&quot;, handler) to de-register</span>
        container.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>But do we really want to do that every time we set up a handler and for each event type?  It also doesn&#8217;t look very elegant (IMHO).  This solution is starting down the right path, though: we need to create the function attached to the node in a different scope.  Care needs to be taken because it&#8217;s easy to just add another reference into the circular reference chain:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> createHandler<span style="color: #009900;">&#40;</span>func<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">function</span> handler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        func.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> handler<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;someElement&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    listen<span style="color: #009900;">&#40;</span>elem<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;onclick&quot;</span><span style="color: #339933;">,</span> createHandler<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>At first glance, this looks like it should work; a trained eye would see that this is just adding another reference into the chain:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">elem.onevent -&gt;
    handler -&gt;
    handler.scope_chain -&gt;
    createHandler_activation_object.func -&gt;
    inner_func -&gt;
    inner_func.scope_chain -&gt;
    outer_activation_object -&gt;
    outer_activation_object.elem -&gt;
    elem</pre></div></div>

<p>We need a way to break that chain.</p>
<h2>Fixing It By Global Reference</h2>
<p>As we&#8217;ve seen, accessing an element from a function through an activation object through the scope chain will cause a leak if it&#8217;s not properly cleaned up or worked around.  However, there&#8217;s an interesting behavior I haven&#8217;t mentioned: a circular reference through the global object through the scope chain will not leak!  This behavior was pointed out to me by <a href="http://allyoucanleet.com/">John David Dalton</a>.  By combining this fact with what we&#8217;ve learned so far, we can create a <a href="http://www.reigndropsfall.net/demos/ie_leak/no_leak_1.html">generic event handling system</a> that the browser will clean up after:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> _cache <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> createWrapper <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">function</span> createLookupHandler<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>id <span style="color: #000066; font-weight: bold;">in</span> _cache <span style="color: #339933;">&amp;&amp;</span> _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #003366; font-weight: bold;">function</span> createWrapper<span style="color: #009900;">&#40;</span>func<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> cid <span style="color: #339933;">=</span> id<span style="color: #339933;">++,</span>
            wrapper <span style="color: #339933;">=</span> createLookupHandler<span style="color: #009900;">&#40;</span>cid<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        _cache<span style="color: #009900;">&#91;</span>cid<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> func<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> wrapper<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> createWrapper<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> listen<span style="color: #009900;">&#40;</span>node<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> wrapper <span style="color: #339933;">=</span> createWrapper<span style="color: #009900;">&#40;</span>handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    _listen<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> normalizeEventName<span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> wrapper<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> wrapper<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> stopListening<span style="color: #009900;">&#40;</span>node<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    _stopListening<span style="color: #009900;">&#40;</span>node<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Let&#8217;s analyze the reference chain created here:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">elem.onclick -&gt;
    createLookupHandler_inner -&gt;
    createLookupHandler_inner.scope_chain -&gt;
    global_object -&gt;
    global_object._cache -&gt;
    _cache -&gt;
    _cache[id] -&gt;
    LeakFunc -&gt;
    LeakFunc.scope_chain -&gt;
    outer_activation_object -&gt;
    outer_activation_object.elem -&gt;
    elem</pre></div></div>

<p>As you can see, we have a circular reference but we&#8217;re going through the global object through a scope chain.  <code>_cache</code> <strong>MUST</strong> be a global variable or reached through the global scope.  Accessing <code>_cache</code> through an activation object through the scope chain will cause a leak.  For instance, the following <a href="http://www.reigndropsfall.net/demos/ie_leak/leak_cache_1.html">will leak</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> createWrapper <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> _cache <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">function</span> createLookupHandler<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>id <span style="color: #000066; font-weight: bold;">in</span> _cache <span style="color: #339933;">&amp;&amp;</span> _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    ...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Why?  <code>_cache</code> is now accessible via the outer function&#8217;s activation object:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">elem.onevent -&gt;
    createLookupHandler_inner -&gt;
    createLookupHandler_inner.scope_chain -&gt;
    createWrapper_inner_activation_object -&gt;
    createWrapper_inner_activation_object._cache -&gt;
    _cache -&gt;
    _cache[id] -&gt;
    LeakFunc -&gt;
    LeakFunc.scope_chain -&gt;
    outer_activation_object -&gt;
    outer_activation_object.elem -&gt;
    elem</pre></div></div>

<p>The following <a href="http://www.reigndropsfall.net/demos/ie_leak/leak_cache_2.html">will also leak</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> _myCache <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> createWrapper <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>_cache<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">function</span> createLookupHandler<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>id <span style="color: #000066; font-weight: bold;">in</span> _cache <span style="color: #339933;">&amp;&amp;</span> _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    ...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>_myCache<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In this example, <code>_myCache</code> is added to the outer function&#8217;s activation object via the arguments (which are added to the activation object):</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">elem.onevent -&gt;
    createLookupHandler_inner -&gt;
    createLookupHandler_inner.scope_chain -&gt;
    createWrapper_inner_activation_object -&gt;
    createWrapper_inner_activation_object._cache -&gt;
    _myCache -&gt;
    _myCache[id] -&gt;
    LeakFunc -&gt;
    LeakFunc.scope_chain -&gt;
    outer_activation_object -&gt;
    outer_activation_object.elem -&gt;
    elem</pre></div></div>

<p>The object used to store the function references <strong>MUST BE REFERENCED THROUGH THE GLOBAL SCOPE</strong>.  My best guess as to why is that somehow IE6 will increase the reference count of a COM+ object (in this case, a DOMNode) when it is referenced through an activation object through the scope chain.  However, it seems that looking up an object through the global scope (which means you&#8217;re ultimately going through the scope chain) doesn&#8217;t have this same effect.  If someone can shed some more light on this, I would be much obliged.  I have set up a <a href="https://gist.github.com/663298">gist to try to explain what is going on</a> in terms of the ES3 spec and I would appreciate any corrections there as well.</p>
<p>The following examples will not leak because they all ultimately look up functions through the global scope:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> createWrapper <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>global<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    global._cache <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">function</span> createLookupHandler<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>id <span style="color: #000066; font-weight: bold;">in</span> _cache <span style="color: #339933;">&amp;&amp;</span> _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// note that we're referencing _cache</span>
                <span style="color: #006600; font-style: italic;">// via the global scope</span>
                _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    ...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> my <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    really<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        long<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                _cache<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> createWrapper <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">function</span> createLookupHandler<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// note that we're not closing around</span>
            <span style="color: #006600; font-style: italic;">// any part of the namespace object</span>
            <span style="color: #003366; font-weight: bold;">var</span> _cache <span style="color: #339933;">=</span> my.<span style="color: #660066;">really</span>.<span style="color: #660066;">long</span>.<span style="color: #003366; font-weight: bold;">namespace</span>._cache<span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>id <span style="color: #000066; font-weight: bold;">in</span> _cache <span style="color: #339933;">&amp;&amp;</span> _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    ...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Event Handlers in Libraries</h2>
<p>I&#8217;ve taken the liberty of writing some leak tests for the latest versions (at the time of this article) of 6 of the most popular JavaScript libraries used today (full disclosure: I am a Dojo committer).  I have included the results of the tests on IE6 version 6.0.3790.3959 running on Windows 2003 (this version doesn&#8217;t have the patch applied to it to fix the circular reference leak).  Note that I have not included which method the library uses to prevent leaks, just if it leaks or not:</p>
<ul>
<li><a href="http://www.reigndropsfall.net/demos/ie_leak/leak_test_dojo.html">Dojo 1.5</a> &#8211; doesn&#8217;t leak</li>
<li><a href="http://www.reigndropsfall.net/demos/ie_leak/leak_test_ext.html">Ext 3.3.1</a> &#8211; <strong>leaks</strong></li>
<li><a href="http://www.reigndropsfall.net/demos/ie_leak/leak_test_jquery.html">jQuery 1.4.4</a> &#8211; doesn&#8217;t leak</li>
<li><a href="http://www.reigndropsfall.net/demos/ie_leak/leak_test_mootools.html">MooTools 1.3.0</a> &#8211; doesn&#8217;t leak</li>
<li><a href="http://www.reigndropsfall.net/demos/ie_leak/leak_test_prototype.html">Prototype 1.7.0.0</a> &#8211; doesn&#8217;t leak</li>
<li><a href="http://www.reigndropsfall.net/demos/ie_leak/leak_test_yui.html">YUI 3.2.0</a> &#8211; <strong>leaks</strong></li>
</ul>
<h2>Conclusion</h2>
<p>From all that we&#8217;ve seen here, the following pattern (or one similar to it) should be used to prevent leaks in event handlers in Internet Explorer without having to use an unload handler:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> _cache <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> createHandler <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> createLookupHandler<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>id <span style="color: #000066; font-weight: bold;">in</span> _cache <span style="color: #339933;">&amp;&amp;</span> _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> createHandler<span style="color: #009900;">&#40;</span>func<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> cid <span style="color: #339933;">=</span> id<span style="color: #339933;">++,</span>
            handler <span style="color: #339933;">=</span> createLookupHandler<span style="color: #009900;">&#40;</span>cid<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        _cache<span style="color: #009900;">&#91;</span>cid<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> func<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> handler<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> createHandler<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
elem.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;onclick&quot;</span><span style="color: #339933;">,</span> createHandler<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.reigndropsfall.net/2010/08/12/dojox-charting-axis-titles/" title="Adding Axis Titles to DojoX Charts">Adding Axis Titles to DojoX Charts</a></li><li><a href="http://www.reigndropsfall.net/2010/07/07/local-modules-with-xdomain/" title="Using Local Modules With a Cross-Domain Dojo Build">Using Local Modules With a Cross-Domain Dojo Build</a></li><li><a href="http://www.reigndropsfall.net/2010/06/15/monkey-patching/" title="Monkey patching">Monkey patching</a></li><li><a href="http://www.reigndropsfall.net/2005/09/13/catching-up/" title="Catching up">Catching up</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2011/01/05/internet-explorer-event-handler-leaks/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Catching up</title>
		<link>http://www.reigndropsfall.net/2005/09/13/catching-up/</link>
		<comments>http://www.reigndropsfall.net/2005/09/13/catching-up/#comments</comments>
		<pubDate>Wed, 14 Sep 2005 00:35:47 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[coaster]]></category>
		<category><![CDATA[internet explorer]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[So much has happened since I last blogged&#8230; I really need to get into the habit of writing more often. My wife had her 21st birthday. It was pretty uneventful. She hasn&#8217;t even had her first drink. She also made her own blog today. I&#8217;m so proud! Work has gotten pretty crazy. I am now [...]]]></description>
			<content:encoded><![CDATA[<p>So much has happened since I last blogged&#8230;  I really need to get into the habit of writing more often.</p>
<p>My <a href="http://pennyfilledthoughts.blogspot.com">wife</a> had her 21st birthday.  It was pretty uneventful.  She hasn&#8217;t even had her first drink.  She also made her own <a href="http://pennyfilledthoughts.blogspot.com">blog</a> today.  I&#8217;m so proud!</p>
<p>Work has gotten pretty crazy.  I am now the webmaster of the company&#8217;s website, which has been fun so far.  Since this isn&#8217;t a hacker site where I can tell people to go stick it if it doesn&#8217;t render in Internet Explorer correctly, I&#8217;ve had to figure out how to get a valid site to work with IE.  Dean Edwards&#8217; <a href="http://dean.edwards.name/IE7/">IE7</a> has helped out a ton.  We also installed a <a href="http://www.jabber.org">Jabber</a> server on the file server so that we have a reliable messaging server to talk to each other (MS Messenger just wasn&#8217;t cutting it).</p>
<p>With all of this happening at work, I&#8217;ve had little time to work on <a href="http://www.coaster-burner.org">Coaster</a> (this is beginning to be quite the standard blog topic for me).  I started working on it again this weekend and I got some code duplication moved to the base classes.</p>
<p>Along with my hacking this weekend, we went and bought a fountain/waterfall thing for the bedroom and an <a href="http://www.homedics.com/prod/detail.aspx?ID=178">alarm clock</a> that plays 6 different nature sounds.  It&#8217;s quite calming and I&#8217;ve found myself spending more time in there reading.  It&#8217;s also been nice to wake up to the rainforest and the ocean.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.reigndropsfall.net/2011/01/05/internet-explorer-event-handler-leaks/" title="Internet Explorer Event Handler Leaks">Internet Explorer Event Handler Leaks</a></li><li><a href="http://www.reigndropsfall.net/2006/04/01/my-current-endeavors/" title="My current endeavors">My current endeavors</a></li><li><a href="http://www.reigndropsfall.net/2006/01/21/updates-2/" title="Updates">Updates</a></li><li><a href="http://www.reigndropsfall.net/2005/11/09/dork-sided/" title="Dork-sided">Dork-sided</a></li><li><a href="http://www.reigndropsfall.net/2005/08/07/updates/" title="Updates">Updates</a></li><li><a href="http://www.reigndropsfall.net/2005/07/14/oh-the-joy-of-technology/" title="Oh the joy of technology">Oh the joy of technology</a></li><li><a href="http://www.reigndropsfall.net/2005/04/07/drivel-and-coaster/" title="Drivel and Coaster">Drivel and Coaster</a></li><li><a href="http://www.reigndropsfall.net/2005/03/24/coaster-site/" title="Coaster site">Coaster site</a></li><li><a href="http://www.reigndropsfall.net/2005/02/20/i-can-almost-hear-it/" title="I can almost hear it&#8230;">I can almost hear it&#8230;</a></li><li><a href="http://www.reigndropsfall.net/2005/02/08/not-quite-the-universe-and-everything/" title="Not Quite The Universe and Everything">Not Quite The Universe and Everything</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2005/09/13/catching-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

