views:

67

answers:

3

Hi All

If I have the following code in two functions of an object:

add: function()
{
    // create trip.
    var trip = new Trip();

    // add the trip using its id.
    this.trips[trip.id] = trip;
},

remove: function(tripId)
{
    // remove trip.
    delete this.trips[tripId];
}

NOTE: The constructor for the Trip object binds a bunch of custom JQuery event handlers to itself.

Will the event handlers bound to the Trip object be automatically destroyed/cleaned up when the Trip object is deleted?

Would the same occur for a dom node if it was removed and had event handlers bound to it?

Also I read that objects are not cleaned up by the garbage collector until all references to them no longer exist, so do the event handlers bound to the object by itself count as references and prevent the object from being cleaned up, even when I am no longer referencing it?

A: 

As far as I know, you can only bind event handlers to nodes, or, in special cases, the window, document, etc. For DOM nodes, the event handlers will be removed. Even if they weren't, they wouldn't be able to be triggered anyway. Deleting the object will remove the event handlers associated with it. The event handlers should not prevent the object from being garbage collected.

SimpleCoder
A: 

Would the same occur for a dom node if it was removed and had event handlers bound to it?

this.handlerClick = function () { ... };

$(this.testDomNode).bind('click', this.handlerClick);

this.testDomNode.parentNode.removeChild(this.testDomNode);

Using the above code and testing with FireQuery in FireFox removing the dom node does not unbind the handler from the event,

it seems you have to explicitly unbind the handler before removing the dom node as follows:

$(this.testDomNode).unbind('click', this.handlerClick);

this.testDomNode.parentNode.removeChild(this.testDomNode);
Gavin
+2  A: 
Anurag
[`$(object).removeData();`](http://api.jquery.com/removeData) is the solution. This will remove any data stored in the jQuery data cache (which includes then event handles) for the object.
gnarf
Thanks, I will test this in the morning and approve the answer if it works. What you have stated about JQuery maintaining a central repository of the events concerns me as this means it is likely the object is not getting deleted either. Please see the following regarding deleting objects: http://stackoverflow.com/questions/742623/deleting-objects-in-javascript if any reference remains to an object it seems the garbage collector will not clean it up, thus if JQuery is storing information about the event and what it is bound to, it could be referencing the object and block collection?
Gavin
Given the above I think internalIndex might also be blocking collection as it references a property of the object? It might be safer to manually inspect $.cache via firebug.
Gavin
@Gavin - As far as I know, jQuery does not hold a **direct** references to objects in this case, but I may be wrong about this. Unless the bound event handler is referring back to the object, it should get deleted and gc'd without problems. Otherwise, you'd have to delete both (event handler, object) to have it garbage collected. Also `internalIndex` in the above case does not interfere with anything since it merely holds an integer value `o[prop]`.
Anurag