G
G
Genome_X2012-06-13 07:37:42
JavaScript
Genome_X, 2012-06-13 07:37:42

How to fix memory leak in IE when deleting and re-creating a variable?

Actually, a question.
I'm using jQuery, I have code like this:

$(&quot;id&quot;).click(function() {<br/>
 if (&quot;perem1&quot; in window && &quot;perem2&quot; in window) {<br/>
 #someaction;<br/>
 delete perem1;<br/>
 delete perem2;<br/>
 } else {<br/>
 perem1= somevalue;<br/>
 perem2= somevalue;<br/>
 };<br/>

Those. the meaning is this: the script checks for the presence of global variables perem1 and perem2, if it does not find them, then it executes the condition after else, i.e. creates global variables and assigns some values ​​to them. When the function is called again, the condition evaluates to TRUE, some #someaction code is executed where the values ​​of these variables are involved, then the variables are deleted.
In browsers FF, Opera everything works correctly. But in IE (version 8 in particular), the code is executed exactly once, when it is reactivated, it gives a memory leak error and, accordingly, does nothing.
As I understand it, there is some problem with deleting variables, in other browsers it works correctly, while in IE it causes some problems, because of which the code only works once.
The question is how to win?
Perhaps it is worth rewriting the code somehow to avoid this delete, but so far I don’t know how.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
M
monolithed, 2012-06-13
@monolithed

Variables declared without the var specifier become properties of the global window object (in the server implementation of global ).
These variables can be used with the delete statement :

property = 1;

top.property;    //1
self.property;   //1
window.property; //1

Those. The delete statement only works on properties of objects.
For example, it is acceptable to remove the properties of built-in objects:
Math.abs;        // function abs() { [native code] }
delete Math.abs; // true
Math.abs;        // undefined

Accordingly, variables declared with the var specifier cannot be deleted:
var variable = 10;
delete variable; // false
variable;        // 10

PS: in consoles like FF , Chrome , the result of the delete instruction may not be correct.

O
Oleg Matrozov, 2012-06-13
@Mear

How do you declare global variables?
Judging by the reference books, there is a big difference between the declarations:

var a = 2;

and
a = 2;

In the first case, the created variable will be a property of the window class and such a variable cannot be deleted directly ( at least some reference books say this ). Those. it may well be that many modern browsers ignore this difference and remove it in both cases, and older IE (still saw the mention of FF 1.5) get problems.

G
Genome_X, 2012-06-13
@Genome_X

Hmm, thanks for the hint and updates, I'll try to predetermine it really, and then not delete it, but change the values.
But, nevertheless, if a variable is simply declared with an expression like someval = 4; (for example), it can not be deleted? Why does delete exist then?

G
Genome_X, 2012-06-13
@Genome_X

So, I just checked, in the latest version of Chrome, everything also works correctly. Those. works in all browsers except IE (I haven't tested it on 9 yet).
It is somehow strange that a global variable cannot be deleted. It turns out, logically, all browsers work incorrectly and only IE gives an error because it knows how to do it right? Somehow this raises doubts.

G
gaelpa, 2012-06-13
@gaelpa

Apart from the fact that it is probably better not to delete such variables, but to transfer them to undefined(and not global ones, but, for example, in a closure), try creating and deleting them with a full indication of the "path", i.e. delete window.myvar1.

G
Genome_X, 2012-06-13
@Genome_X

Aaaaaaa, well, cool ... :(
In general, it works in IE9 version. Doesn't work in IE lower versions (8th and lower).
Looked under the debugger, writes "Object doesn't support this property or method". Swears on the line after "if ("perem1" in window && "perem2" in window)", where I have some manipulations with variables.
Damn. Ambush. The
idea, by the way, was not bad, to play around not by deleting, but by setting the value of the variable to null It
works, in principle, but I still can’t figure out how to change the condition (“perem1” in window && “perem2” in window) for the first and subsequent clicks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question