Opened 12 years ago

Closed 12 years ago

#2364 closed defect (duplicate)

memory leak in dojo.event.connect/dojo.event.disconnect

Reported by: guest Owned by: anonymous
Priority: high Milestone: 0.9
Component: Core Version: 0.4.1
Keywords: event connect disconnect memory leak Cc:
Blocked By: Blocking:

Description

Nodes being dynamically created, connected, unconnected and destroyed after all will leak memory both in IE and FF. The loss is small but crucial for single-page applications where a lot of nodes are being created and connected via dojo.event.connect.

See the example code below or check out the live demo on http://web3.bag.ch and watch your browser's memory consumption grow in Process Explorer or a similar tool.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
		<title>Testcase Context Menu Memory Leak</title>
		<script type="text/javascript" src="dojo/dojo.js" ></script>

		<script language="JavaScript" type="text/javascript">
			dojo.require("dojo.event.*");
			dojo.hostenv.writeIncludes();
		</script>

		<script language="JavaScript" type="text/javascript">

			var amount = 0;

			function Test() {

				// create element
				var element = document.createElement('div');
				document.body.appendChild(element);
				amount++;
				document.getElementById('amount').innerHTML = amount;

				// connect test function on onclick event				
				dojo.event.connect(element, 'onclick', this, 'MyOnclickFunction');

				// disconnect event
				dojo.event.disconnect(element, 'onclick', this, 'MyOnclickFunction');

				// remove element
				document.body.removeChild(element);

			}
			
			function StartTest() {
				task = window.setInterval("Test()", 100);
				document.getElementById('state').innerHTML = 'Running';
			}

			function StopTest() {
				window.clearInterval(task);
				document.getElementById('state').innerHTML = 'Not running';
			}
			
			function MyOnclickFunction() {
				return true;
			}
			

		</script>

	</head>
	<body style="font-family:Arial;font-size:13px;">
		<p>
			<button name="stressTest" type="button" onclick="StartTest();">Start Test</button> 
			<button name="stressTest" type="button" onclick="StopTest();">Stop Test</button>
		</p>
		<p><b>State:</b> <span id="state">Not running</span> / <b>Amount of elements created:</b> <span id="amount">0</span></p>
	</body>
</html>

Change History (6)

comment:1 Changed 12 years ago by Eugene Lazutkin

I cannot reproduce it on FF2/Windows with posted code.

comment:2 Changed 12 years ago by guest

Sorry, didn't mention I reproduced it in FF 1.5/Win and in IE6 + IE7. Memory consumption grows in FF slower though and starts after having created about 150 nodes or so...

comment:3 Changed 12 years ago by Adam Peller

Is this a dup of #1727? Is dojo.event.connect at fault or node.removeChild? Can you post an e-mail address in case we need to contact you later?

comment:4 Changed 12 years ago by guest

Sorry - seems to be my fault. The following lines fixed the leak:

// remove element
/* document.body.removeChild(element); (leak was here) */

var node = dojo.dom.removeNode(element);
dojo.dom.destroyNode(node);

Just wondering: Wouldn't it be dojo.event.disconnect's job to *really* remove everything from a previously connected node in order to prevent memory leaks (like setting events to null etc.)? This may be a stupid question since I don't know exactly what's necessary to prevent leaks for all browsers, but using dojo.event.disconnect I thought I'm doing everything correctly...

Thx! (m dot reufer at bag dot ch)

comment:5 Changed 12 years ago by dylan

Milestone: 0.9
Priority: highnormal

comment:6 Changed 12 years ago by Adam Peller

Resolution: duplicate
Status: newclosed
Note: See TracTickets for help on using tickets.