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 2587 acoder 16,027
Recognized Expert Moderator MVP
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>
-
-
acoder 16,027
Recognized Expert Moderator MVP
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
acoder 16,027
Recognized Expert Moderator MVP
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>
-
rnd me 427
Recognized Expert Contributor -
-
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
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 -AA flag. In
another news group I came across some interesting (ok scarey)
information regarding memory leaks in the STL list<...> container. I
have compiled and executed the following code and verified that this
does in fact leak on my system.
|
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 connection to the pool,
SQLDisconnect, and after the time that the connection remains in pool
expires we see the memory usage drop by about 28K.
Our basic steps for a select are:
SQLAllocHandle(SQL_HANDLE_DBC, hGblEnv, &hdbc);
|
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. So I'm wondering if I'm
missing something?
My test code is as follows:
function myObj() {
var req = new Object();
req.temp = 0;
|
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 querying to
methods in a single static class, so whenever I need data, I pass a SQL
string to a method and I am passed a datareader or else individual values
(string, integer, etc).
There is a horrible memory leak in this application. Just one...
|
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 calculations and stores elements in a list. After
that, a sorting algorithm is used over that list.
Two functions are called before the sorting process:
|
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 that I found inside of a
string.
Any ideas?
|
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 page:
<html>
<head>
<SCRIPT LANGUAGE="JavaScript" SRC="Engine.js" ></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" SRC="Sarissa.js" </SCRIPT>
</head>
|
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. However, I am looking
at the Task Manager and I don't see the MemUsage column decreasing even
after an hour or two. I know that TaskManager may not be the best place
to see what is the true way to gauge memory usage and/or presense of
memory leaks. So...
|
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 throwing a
std::exception with a string object.
This is the test program
=============================================================================
#include <sstream>
#include <iostream>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |