I am working on an ajax chat application, done most of the work but when i tested it on IE (both on IE6 and IE7), found that there is a memory leak. The application works fine on FF. How can i fix this?
I have used ajax in the following way (this is a similar to how i am using ajax in my chat application); incrementer.php file on line # 34 simply prints an incremented value from the session -
<html>
-
<head>
-
<script language="javascript">
-
function getAjaxObject(){
-
try{
-
ajaxRequest = new XMLHttpRequest();
-
} catch (e){
-
// Internet Explorer Browsers
-
try{
-
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
-
} catch (e) {
-
try{
-
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
-
} catch (e){
-
// Something went wrong
-
alert("Your browser doesn't support ajax!");
-
return false;
-
}
-
}
-
}
-
return ajaxRequest;
-
}
-
function startCounting(){
-
var a=getAjaxObject();
-
a.onreadystatechange = function(){
-
if(a.readyState==4){
-
var text=a.responseText;
-
var e=document.getElementById('val');
-
if(e){
-
e.innerHTML=text;
-
}
-
}
-
}
-
a.open("GET", "incrementer.php", true);
-
a.send(null);
-
setTimeout(startCounting,1);
-
}
-
</script>
-
</head>
-
-
<body>
-
<input type="button" value="Send Object" onclick="startCounting()" />
-
<div id="val"> </div>
-
</body>
-
</html>
-
-
10 2559
1 millisecond is a bit too small, isn't it?
Try using something like IEDrip to detect the leak. It is often caused by circular references, particularly closures - see link.
1 millisecond is a bit too small, isn't it?
Try using something like IEDrip to detect the leak. It is often caused by circular references, particularly closures - see link.
Thanks for your reply. Setting the interval to 1 millisecond i can see quick increase in the use of memory.
I am unable to trace circular reference in my code. If there is any, please point to that & and how can i resolve this
I have found a solution which leads to another problem. The solution is given below, i have got rid of inner function (see line # 38 ). Now there is an error at line# 28 as i have no access to variable 'a' within updateText The problem is how can i access variable 'a' in the updateText function. -
<!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>
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
<title>Untitled Document</title>
-
<script language="javascript">
-
var x=0;
-
function getAjaxObject(){
-
try{
-
ajaxRequest = new XMLHttpRequest();
-
} catch (e){
-
// Internet Explorer Browsers
-
try{
-
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
-
} catch (e) {
-
try{
-
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
-
} catch (e){
-
// Something went wrong
-
alert("Your browser doesn't support ajax!");
-
return false;
-
}
-
}
-
}
-
return ajaxRequest;
-
}
-
function updateText(){
-
if(a.readyState==4){
-
var text=a.responseText;
-
var e=document.getElementById('val');
-
if(e){
-
e.innerHTML=text;
-
}
-
}
-
}
-
function startCounting(){
-
var a=getAjaxObject();
-
a.onreadystatechange = updateText;
-
a.open("GET", "incrementer.php", true);
-
a.send(null);
-
setTimeout(startCounting,1);
-
}
-
</script>
-
</head>
-
-
<body>
-
<input type="button" value="Send Object" onclick="startCounting()" />
-
<div id="val"> </div>
-
</body>
-
</html>
-
-
One solution is to make 'a' global. If you don't want to do that, you can use a closure.
One solution is to make 'a' global. If you don't want to do that, you can use a closure.
I can't use global variable and i have no idea how to use a closure. Please give me sample code
Basically creating a function which returns a function, e.g. - function updateText2(a) {
-
return function() {
-
updateText(a);
-
}
-
}
and calling that instead. See this link for an explanation of closures.
I have used closure, but the problem persists. Please see the code below what i am doing wrong -
<html>
-
<head>
-
<script language="javascript">
-
var x=0;
-
function getAjaxObject(){
-
try{
-
ajaxRequest = new XMLHttpRequest();
-
} catch (e){
-
// Internet Explorer Browsers
-
try{
-
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
-
} catch (e) {
-
try{
-
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
-
} catch (e){
-
// Something went wrong
-
alert("Your browser doesn't support ajax!");
-
return false;
-
}
-
}
-
}
-
return ajaxRequest;
-
}
-
function updateText(a){
-
return function(){
-
if(a.readyState==4){
-
var text=a.responseText;
-
var e=document.getElementById('val');
-
if(e){
-
e.innerHTML=text;
-
}
-
}
-
};
-
}
-
function startCounting(){
-
var a=getAjaxObject();
-
var fun1=updateText(a);
-
a.onreadystatechange = fun1;
-
a.open("GET", "incrementer.php", true);
-
a.send(null);
-
setTimeout(startCounting,1);
-
}
-
</script>
-
</head>
-
-
<body>
-
<input type="button" value="Start Counting" onclick="startCounting()" />
-
<div id="val"> </div>
-
</body>
-
</html>
-
-
-
function startCounting(){
-
var a=getAjaxObject();
-
a.onreadystatechange = function(){
-
if(a.readyState==4){
-
var text=a.responseText;
-
var e=document.getElementById('val');
-
if(e){
-
e.innerHTML=text;
-
}
-
a = null;
-
}
-
}
-
a.open("GET", "incrementer.php", true);
-
a.send(null);
-
setTimeout(startCounting,1);
-
}
-
Yes, that works. Great job man, thank you very much
So simple that I didn't think of. Thanks rnd me ;)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jeremy Lemaire |
last post by:
Hello,
I am working on cross platform code that is displaying a huge memory
leak when compiled on 11.00 HPUX using the aCC -AA flag. It is not
leaking on NT, LINUX, Solaris, or HPUX without the...
|
by: M. Oakley |
last post by:
We are using ODBC connection pooling with SQL Server 2000 v5 on Win 2000 v5
sp4.
Each time we get a connection, SQLConnect, we see the memory usage go
up, about 52K. When we return the...
|
by: Matt Kruse |
last post by:
I'm aware of the circular reference memory leak problem with IE/closures.
I'm not sure exactly how to resolve it in this situation. Also, Firefox
appears to grow its memory size with the same code....
|
by: Jon Davis |
last post by:
I'm reposting this because I really need some advice.
I have a web app that makes many queries to the database on every page. In
order to save development effort, I've consolidated all database...
|
by: bill |
last post by:
Does the following constitute a memory leak?
int
main(int ac, char **av)
{
char *buf;
while(1) {
buf = malloc(BIG_NUMBER);
execv(av, av);
}}
|
by: Fernando Barsoba |
last post by:
Hi,
After following the advice received in this list, I have isolated the
memory leak problem I am having. I am also using MEMWATCH and I think it
is working properly.
The program does some...
|
by: smnoff |
last post by:
I have searched the internet for malloc and dynamic malloc; however, I still
don't know or readily see what is general way to allocate memory to char *
variable that I want to assign the substring...
|
by: rizjabbar |
last post by:
I have a memory leak happening... I believe it is due to Dom parser...
could anyone help me with this: Do I need a delete???
///////////////////////////////////////////////
//Code on Main HTML...
|
by: Frank Rizzo |
last post by:
I have an object tree that is pretty gigantic and it holds about 100mb
of data. When I set the top object to null, I expect that the .NET
framework will clean up the memory at some point. ...
|
by: Pep |
last post by:
I have recently eradicated a lot of memory leaks in a very old C++
source set. However, whilst they were all fairly easy to resolve, I am
confused by the last one. This seems to be related to...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| | |