Solution to memory leakage in IE6

Source: Internet
Author: User
Try finally to prevent memory leakage

<Div id = "d1"> </div>
<SCRIPT>
Function createbutton (){
VaR OBJ = Document. createelement ("button ");
OBJ. innerhtml = "Click me! ";
OBJ. onclick = function (){
// Process the Click Event
}
OBJ. onmouseover = function (){
// Process the Mouseover event
}
Return OBJ; // the object to be created must be returned, so you cannot set OBJ to null directly. after return, obj is a local variable and cannot be referenced from htmlelement externally. problems will occur in IE
}
VaR button = Document. getelementsbyid ("d1"). appendchild (createbutton ());
Button. Do something ();
Button. Do something ();
........
Something. Something (button );
......
</SCRIPT>

This method causes 100% Memory leakage in IE.

Try finally is easy to solve.
Function createbutton (){
VaR OBJ = Document. createelement ("button ");
OBJ. innerhtml = "Click me! ";
OBJ. onclick = function (){
// Process the Click Event
}
OBJ. onmouseover = function (){
// Process the Mouseover event
}
Try {
Return OBJ;
} Finally {
OBJ = NULL; // This statement is executed after return. The effect solves the problem of setting OBJ to null after return.
}
}

In a function or method, in fact, there are many places where such an optional return value is required, and finally some things are executed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.