views:

38

answers:

2

Hey Everyone,

I've noticed some memory leaks in an app i'm building, after playing around with for a while FF will start to use up more and more memory (upwards to 1 000 000 k).

I've done some research and found that if i do a $(selector).html(some stuff) to replace the contents of something the jQuery handlers from the elements previous content will not be removed and cause some problems, i've fixed all those.

the question i have is if i destroy a dialog with $(mydialog).dialog('destroy'); will the handlers that were attached to various elements that were in that dialog be removed?

Thanks!

+3  A: 

No, they will not be removed, the dialog element itself will be returned to it's previous state, the elements inside aren't touched.

The dialog widget itself, the buttons, title bar, close button, etc. are cleaned up, but the element you turned into a dialog is not affected and not cleaned up. You need to either .empty() or .remove() the whole element for that.

Nick Craver
ahhh, interesting. if i did a $(this).remove(); in the close callback that would take care of it yes?
Patricia
@Patricia - Yup, exactly
Nick Craver
fantastic! thanks :D
Patricia
+1  A: 

I'm not sure if destroying a dialog with the provided method removes any contained event handlers, but you can always do:

$(selector).empty();

instead of:

$(selector).html('blahblah'); // or .html('');

and that will get rid of any event handlers bound to any replaced elements, and thus avoid memory leaks.

karim79
yea, anywhere that i had a $(selector).html(soem stuff) i changed it to being $(selector).empty().html(some stuff) to fix those problems. i was more wondering what exactly destroy did.
Patricia