Holtorf
Holtorf

Reputation: 1491

Where is the memory leak coming from?

I am using an old version of extjs to debug my js code. I make this call in my code:

Ext.History.add("text");

Internally Ext.History is implemented like this:

Ext.History = (function () {/* code goes here. */ })();

The problem is that calling Ext.History.add() causes a memory leak on IE. So assume I probably want to stop using the enclosure and do something like this:

function History () {
   /* code */
}
Ext.History = History;

But this causes errors.

Edit: the error came from the add method inside of the history object.

Upvotes: 0

Views: 245

Answers (1)

RobG
RobG

Reputation: 147343

What evidence do you have of a memory leak? Most have been fixed in IE, it certainly hasn't been a hot topic for a few years. IE 6 and early IE 7 had issues with circular references involving DOM objects, but they've been pretty much fixed, even in those older versions.

Anyhow, in the following:

> Ext.History = (function () {/* code goes here. */ })(); 

the immediately invoked function expression (IIFE) on the right only has a closure to the outer scope (which appears to be glbal code). You don't show where any problematic closures are, so it's very difficult to comment.

Presumably the IIFE returns an object with an add property that is a function, and that function has a closure to variables declared in the IIFE, e.g.

Ext.History = (function () {
  var foo = 'foo';
  return {
    add: function(bar) {
      return foo + ' ' + bar;
    }
  }
})(); 

You may be able to replace the whole thing with an object and direct properties, e.g. the following will seem like the same as the above, except it access properties of History rather than closures:

Ext.History = {

  foo: 'foo',

  add: function (bar) {
    return this.foo + ' ' + bar;
  }
};

Note that the this value of add must be History (e.g. call it as Ext.History.add()) otherwise it won't work. Also, variables with names the same as current properties need to be given new names.

If you post the whole code (or a reasonable chunk that shows the pattern) a fix might be possible. Alternatively, look in new versions and see what they've done to fix it.

Upvotes: 1

Related Questions