Here's my stab at a fix that cleans up namespaced-event-handlers in in
the jQuery.cache object on 'unload' event
(jquery-1.3.2-namespaced-events-leak-fix.patch).
To validate my changes, I alert-ed out the contents of jQuery.cache
(jquery-1.3.2-unload-debug-out.patch).
How to test:
------------------
# save a backup copy
cp jquery-1.3.2.js jquery-1.3.2.js.bak
# apply patch for namespaced-events-leak fix
patch -p0 jquery-1.3.2.js <jquery-1.3.2-namespaced-events-leak-fix.patch
# apply patch for debugging output for the 'unload' event-handler
patch -p0 jquery-1.3.2.js <jquery-1.3.2-unload-debug-out.patch
# test it out with some sample HTML like so (load it, and hit reload
to trigger the 'unload' event):
<html>
<head>
<script type="text/javascript" src="/js/jquery-1.3.2.js"></script>
<script>
$(document).ready(
function () {
$('#b1').bind('click.foo', function() { alert('b1'); });
$('#b1').bind('click.bar', function() { alert('b1'); });
}
);
</script>
</head>
<body>
<button id="b1">test</button>
</body>
</html>
# when satisfied, rollback the debug output patch
patch -p0 -R jquery-1.3.2.js <jquery-1.3.2-unload-debug-out.patch
------------------
Questions, comments, or if there's some other preferred way of
submitting a fix, please let me know. Thanks.
--Mike
On Sat, Apr 25, 2009 at 1:09 PM, Mike Park <[email protected]> wrote:
> new thread, as requested (original message below):
> -------
>
> There is definitely a problem with the way jQuery (I'm using 1.3.2)
> handles cleaning up namespaced events (I'm tracing the 'unload' event
> codepath). The problem from what I can tell seems to be in
> jQuery.event.remove:
>
> 2540 // Handle multiple events seperated by a space
> 2541 // jQuery(...).unbind("mouseover mouseout", fn);
> 2542 jQuery.each(types.split(/\s+/), function(index, type){
> 2543 // Namespaced event handlers
> 2544 var namespaces = type.split(".");
> 2545 type = namespaces.shift();
> 2546 var namespace = RegExp("(^|\\.)" + namespaces.slice
> ().sort().join(".*\\.") + "(\\.|$)");
>
> ...during the 'unload' event-handler codepath, this code snippet here
> is expecting the passed in 'types' argument to contain the namespaced
> event-identifier (ex. 'click.foo'), but instead it is getting the
> common event-identifier instead (ex. 'click').
>
> You can firebug-trace this yourselves with the simple examples I put
> up here:
> http://emparq.com/ex/ex-good.html
> http://emparq.com/ex/ex-bad.html
>
> ...you'll notice the only difference between these two examples is
> that one binds 'click.foo' and the other binds 'click'. In the bad
> example, hitting reload constantly will see the process memory useage
> go up (in essense, a leak) due to 'jQuery.event.remove' not properly
> cleaning up the namespaced 'click.foo' event), while the good example
> will not suffer the same results.
>
> I'm still new-ish to jQuery and I'm not ready to hazard a solution
> just yet, but I'm guessing something either needs to be done with the
> way 'jQuery.event.remove' handles the value of the 'types' argument,
> or this code here:
>
> 2531 for ( var type in events )
> 2532 this.remove( elem, type + (types || "") );
>
> ...needs to be modified to properly put those namespaced suffixes back
> into the second argument. I'm not familiar enough with all of the use-
> cases that this function handles, so I don't know which is the best
> approach.
>
> Can anyone else shed some light on this subject?
>
>
> --Mike
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---
2526c2526
< var events = jQuery.data(elem, "events"), ret, index;
---
> var events = jQuery.data(elem, "events"), index;
2531c2531,2553
< for ( var type in events )
---
> // - for each event-handler type
> // (ex. 'click', 'mouseover', 'keydown', etc.)
> for ( var type in events ) {
> var eType = events[type];
> var allTypes = '';
>
> // - event-handler type can have multiple functions bound
> // pick up each namespace, and build a space-delimited list
> // (ex. 'click.foo click.bar click.baz')
> for (var handlerId in eType) {
> var handlerNamespace = eType[handlerId].type;
> if (typeof handlerNamespace === "string") {
> allTypes = (jQuery.trim(handlerNamespace).length > 0)
> ? allTypes + " " + type + "." + handlerNamespace
> : allTypes + " " + type;
> }
> }
> // - if namespaced-event-handlers found, use them in place of
> // original 'type' value
> allTypes = jQuery.trim(allTypes);
> if (allTypes != type)
> type = allTypes;
>
2532a2555
> }
2564,2565c2587,2588
< for ( ret in events[type] ) break;
< if ( !ret ) {
---
> for ( var fn in events[type] ) break;
> if ( !fn ) {
2572c2595
< ret = null;
---
> fn = null;
2580,2581c2603,2604
< for ( ret in events ) break;
< if ( !ret ) {
---
> for ( var evt in events ) break;
> if ( !evt ) {
3129,3130c3129,3132
< jQuery( window ).bind( 'unload', function(){
< for ( var id in jQuery.cache )
---
> jQuery( window ).bind( 'unload', function(){
> var foo = [];
> var foo2 = [];
> for ( var id in jQuery.cache ) {
3132c3134,3136
< if ( id != 1 && jQuery.cache[ id ].handle )
---
> if ( id != 1 && jQuery.cache[ id ].handle ) {
> foo[foo.length] = jQuery.cache[ id ].handle.elem.nodeName +
> '::' + jQuery.cache[ id ].handle.elem.className;
3133a3138,3149
> }
> }
> for ( var id in jQuery.cache ) {
> // Skip the window
> if ( id != 1 && jQuery.cache[ id ].handle ) {
> foo2[foo2.length] = jQuery.cache[ id ].handle.elem.nodeName +
> '::' + jQuery.cache[ id ].handle.elem.className;
> }
> }
>
> alert('d00 - BEFORE CLEANUP:\n' + foo.join('\n') + '\n------\n'
> + 'd01 - AFTER CLEANUP:\n' + foo2.join('\n'));
3134a3151,3156
> //jQuery( window ).bind( 'unload', function(){
> // for ( var id in jQuery.cache )
> // // Skip the window
> // if ( id != 1 && jQuery.cache[ id ].handle )
> // jQuery.event.remove( jQuery.cache[ id ].handle.elem );
> //});