Should I use removeEventListener to unbind callback functions after use in order to avoid memory leaks or keep memory usage as low as possible? Or it's will be done automatically?
For example:
window.addEventListner('open', windowOpened); window.addEventListner('close', windowClosed); function windowClosed() { // Should I remove callbacks after use? window.removeEventListener('open', windowOpened); window.removeEventListener('close', windowClosed); }
4 Answers
This question is bothering me too, nobody seems to know :(
Well, my take is that window events don't need to be, since they will stop firing when the window is closed. However, App events and events coming from outside the window should be, otherwise you might get errors when it tries to send them to null.
I just spent days working on memory leaks. So what i can tell you is that you dont need to removeEventListener for events on the window itself or its objects.
But be careful with Ti.App.addEventListener, If you dont remove them in the 'close' event of the window, then all proxys in the window wont get released! I spent a whole day tracking this one!
I've checked on Instruments that the window actually close, but still i explicitly erase/remove/close/dispose everything to avoid memory leaks and keep memory low.
win.addEventListener('close',function(e){ btn_close.removeEventListener('touchend',listener1); area1.removeEventListener('click',listener2); btn_close = null; area1 = null; win = null; // Not sure if this is necesary, but anyway i still do it... });
Your Answer
Think you can help? Login to answer this question!