Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 67003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:11:30+00:00 2026-05-10T19:11:30+00:00

The code is rather long yet simple: 100 leaky JavaScript objects are created. 10

  • 0

The code is rather long yet simple:

  • 100 leaky JavaScript objects are created.
  • 10 leaky elements are created from the JS objects.
  • 1 element is removed and 1 is added 10000 times.

I assume that the detachEvent call is not functioning properly. Also, if you change this.eventParams from an array to a simple variable, the leak goes away. Why?

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml"> <head>     <title>Memory Leak With Fix</title>     <style type="text/css">       .leakyEle        {         border: solid 1px red;         background-color: Gray;       }     </style>     <script type="text/javascript">      /******************************* MAIN ********************************/ var leakObjArray = new Array(); AddEvent(window, 'load', Startup, false);  function Startup() {              for(var i=0; i<100; i++) {     leakObjArray.push(new LeakyObj(i));   }      for(var j=0; j<10; j++) {     leakObjArray[j].CreateLeakyEle();   }        var container = document.getElementById('Container');   AddEvent(container, 'click', Run, false);   alert('Close this dialog and click the document to continue.'); }  function Run() {   var k = 0;   var l = 10;      for(var m = 0; m<10000; m++) {     leakObjArray[k].DestroyLeakyEle();     leakObjArray[l].CreateLeakyEle();     if(k<leakObjArray.length - 1) {       k++;     } else {       k = 0;     }     if(l<leakObjArray.length - 1) {       l++;     } else {       l = 0;     }   }      for(var i=0; i<leakObjArray.length; i++) {     leakObjArray[i].DestroyLeakyEle();   }   alert('Test Complete.'); }  /******************************* END MAIN ********************************/  /******************************* LEAKY OBJECT ********************************/ function LeakyObj(id) {   this.id = id;   this.leakyEle = null;   this.containerEle = document.getElementById('Container');   this.clicked = false;   this.eventParams = new Array(); }  LeakyObj.prototype.CreateLeakyEle = function() {   var leakyEle = document.createElement('div');   leakyEle.id = 'leakyEle' + this.id;     leakyEle.className = 'leakyEle';              leakyEle.innerHTML = this.id + ' --- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' +     '<br/>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' +     '<br/>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' +     '<br/>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' +     '<br/>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';       this.leakyEle = leakyEle;      var _self = this;   this.eventParams.push(AddEventWithReturnParams(this.leakyEle, 'click', function() { _self.EventHandler(); }, false));      this.containerEle.appendChild(leakyEle); }  LeakyObj.prototype.DestroyLeakyEle = function() {   if(this.leakyEle != null) {     this.containerEle.removeChild(this.leakyEle);     for(var i=0; i<this.eventParams.length; i++) {             RemoveEventOverload(this.eventParams[i]);     }     this.leakyEle = null;   } }  LeakyObj.prototype.EventHandler = function() {   this.leakyEle.style.display = 'none';   this.clicked = true; }  /******************************* END LEAKY OBJECT ********************************/  /******************************* GENERAL FUNCS ********************************/      function AddEvent(elm, evType, fn, useCapture){   var success = false;   if(elm.addEventListener) {     if(evType == 'mousewheel') evType = 'DOMMouseScroll';     elm.addEventListener(evType, fn, useCapture);         success = true;   } else if(elm.attachEvent) {     if(evType == 'mousewheel') {       window.onmousewheel = document.onmousewheel = fn;       success = true;     } else {       var r = elm.attachEvent('on' + evType, fn);       success = r;     }   } else {     success = false;   }   elm = null;   return success; }  function AddEventWithReturnParams(elm, evType, fn, useCapture) {   var eventParams = new EventParams(elm, evType, fn, useCapture);   AddEvent(elm, evType, fn, useCapture);   return eventParams; }  function RemoveEvent(elm, evType, fn, useCapture) {   if(elm) {     if(elm.removeEventListener) {     elm.removeEventListener(evType, fn, useCapture);         return true;   } else if(elm.detachEvent) {       var r = elm.detachEvent('on' + evType, fn);         return r;   } else {     debugger;   }     } }  function RemoveEventOverload(eventParams) {   if(eventParams) {     return RemoveEvent(eventParams.element, eventParams.eventType, eventParams.handler, eventParams.capture);   } }  function EventParams(elm, evType, fn, useCapture) {   return {     element: elm,     eventType: evType,     handler: fn,     capture: useCapture   } } /******************************* END GENERAL FUNCS ********************************/     </script> </head> <body> <div id="Container"></div> </body> </html> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:11:31+00:00Added an answer on May 10, 2026 at 7:11 pm

    looks like you’re pushing stuff onto the eventParams array inside CreateLeakyEle, but never removing it? Is that right?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I find when describing something to clients use a metaphor… May 11, 2026 at 11:24 am
  • added an answer TFS uses windows authentication, so it's as secure as your… May 11, 2026 at 11:24 am
  • added an answer Subclassing is the best option. I would subclass your main… May 11, 2026 at 11:24 am

Related Questions

The code is rather long yet simple: 100 leaky JavaScript objects are created. 10
Is there a way to tidy-up the following code, rather than a series of
IIS is literally sending <?php ... ?> code to the browser rather then executing
The code is return min + static_cast<int>(static_cast<double>(max - min + 1.0) * (number /
The code is, set VAR=before if %VAR% == before ( set VAR=after; echo %VAR%
When code is branched in TFS using the branch method, is the code physically
im re-factoring php on zend code and all the code is full of $_GET[this]
M sending the parameters from the jsp page, the code is <s:url id=url action=searchAction>
I'm the IIS admin, not the developer on this site, so the code is
In Java, the idiomatic way to declare critical sections in the code is the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.