473,396 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Closures memory leak

Hi,

Do I understand correctly from
http://jibbering.com/faq/faq_notes/closures.html that the following will
create a memory leak in IE?
function createMemoryLeak(id)
{
var el = document.getElementById(id);
el.onclick = function()
{
return false;
}
}

If not, could someone give an example that would create a memory leak?
Jul 28 '05 #1
2 1752
On 28/07/2005 11:48, Robert wrote:
Do I understand correctly from
http://jibbering.com/faq/faq_notes/closures.html that the following will
create a memory leak in IE?

function createMemoryLeak(id)
{
var el = document.getElementById(id);
el.onclick = function()
{
return false;
}
}


Yes. The element, el, has a property, onclick, which is a function
reference. In the scope chain of that function exists a reference to the
same element, so you now have a loop. The simple fix is to assign null
to el once the listener assignment is complete.

If, for some reason, you needed to keep a reference to el, you would
need to break the loop when the unload event is dispatched. For instance:

var global = this;

function avoidMemoryLeak(id) {
var el;

if(document.getElementById) {
el = document.getElementById(id);
}
if(el) {
el.onclick = function() {
return false;
};

global.onunload = function() {
el = global.onunload
= null;
};
}
}

But, here you wouldn't need to worry as the this operator could be used
within the listener, rather than el.

If you'd end up producing many closure instances where memory leaks may
occur (which could simply mean calling avoidMemoryLeak several times),
then this becomes unmanageable in its current state. Richard Cornford
has produced code that can help in this regard:

<URL:http://www.litotes.demon.co.uk/example_scripts/finalizer.html>

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jul 28 '05 #2
Michael Winter wrote:
If, for some reason, you needed to keep a reference to el, you would
need to break the loop when the unload event is dispatched.


Well, the page I am working on is altered dynamically using XML
requests. So unload is not good enough. However I will be able to avoid
the memory leak by calling another function that returns a function and
assign it to the event handler.
Jul 28 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Matt Kruse | last post by:
I'm aware of the circular reference memory leak problem with IE/closures. I'm not sure exactly how to resolve it in this situation. Also, Firefox appears to grow its memory size with the same code....
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
10
by: Emre Sevinc | last post by:
Take a look at the following snippet: <html> <head> <script> function add(elementId) { var container = document.getElementById(elementId); for (var i = 0; i < 10; i++) { var elt =...
2
by: Robert | last post by:
Hello javascript group readers, I have a question regarding how to prevent memory leaks in Internet Explorer when using closures. I already knew about the circular reference problem, and until...
14
by: Khookie | last post by:
Woah... is it just me or do C programmers don't bother talking about how cool C can be (compared to Lisp, Haskell, etc.) - functionally speaking? // Lexical scoping - via nested functions...
1
by: Jeremy | last post by:
I'm working on an application that does some DOM manipulation. I want to add a mechanism for "Undo" so that the user can revert to the previous state after performing a mistaken action. Simple...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.