Connecting Tech Pros Worldwide Help | Site Map
Reply
 
LinkBack Thread Tools Search this Thread
  #1  
Old September 5th, 2008, 05:55 AM
rizwan6feb's Avatar
Member
 
Join Date: Jul 2007
Posts: 103
Default IE memory leak, how to resolve?

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

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="javascript">
  4.     function getAjaxObject(){
  5.         try{
  6.             ajaxRequest = new XMLHttpRequest();
  7.         } catch (e){
  8.             // Internet Explorer Browsers
  9.             try{
  10.                 ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  11.             } catch (e) {
  12.                 try{
  13.                     ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
  14.                 } catch (e){
  15.                     // Something went wrong
  16.                     alert("Your browser doesn't support ajax!");
  17.                     return false;
  18.                 }
  19.             }
  20.         }
  21.         return ajaxRequest;    
  22.     }
  23.     function startCounting(){
  24.         var a=getAjaxObject();
  25.         a.onreadystatechange = function(){
  26.             if(a.readyState==4){
  27.                 var text=a.responseText;
  28.                 var e=document.getElementById('val');
  29.                 if(e){
  30.                     e.innerHTML=text;
  31.                 }
  32.             }
  33.         }
  34.         a.open("GET", "incrementer.php", true);
  35.         a.send(null);
  36.         setTimeout(startCounting,1);
  37.     }
  38. </script>
  39. </head>
  40.  
  41. <body>
  42.     <input type="button" value="Send Object" onclick="startCounting()" />
  43.     <div id="val">    </div>
  44. </body>
  45. </html>
  46.  
  47.  
Reply
  #2  
Old September 5th, 2008, 03:50 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,964
Default

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.
Reply
  #3  
Old September 6th, 2008, 05:26 AM
rizwan6feb's Avatar
Member
 
Join Date: Jul 2007
Posts: 103
Default

Quote:
Originally Posted by acoder
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
Reply
  #4  
Old September 6th, 2008, 06:47 AM
rizwan6feb's Avatar
Member
 
Join Date: Jul 2007
Posts: 103
Default

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.
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. <script language="javascript">
  7.     var x=0;
  8.     function getAjaxObject(){
  9.         try{
  10.             ajaxRequest = new XMLHttpRequest();
  11.         } catch (e){
  12.             // Internet Explorer Browsers
  13.             try{
  14.                 ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  15.             } catch (e) {
  16.                 try{
  17.                     ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
  18.                 } catch (e){
  19.                     // Something went wrong
  20.                     alert("Your browser doesn't support ajax!");
  21.                     return false;
  22.                 }
  23.             }
  24.         }
  25.         return ajaxRequest;    
  26.     }
  27.     function updateText(){
  28.         if(a.readyState==4){
  29.             var text=a.responseText;
  30.             var e=document.getElementById('val');
  31.             if(e){
  32.                 e.innerHTML=text;
  33.             }
  34.         }
  35.     }
  36.     function startCounting(){
  37.         var a=getAjaxObject();
  38.         a.onreadystatechange = updateText;
  39.         a.open("GET", "incrementer.php", true);
  40.         a.send(null);
  41.         setTimeout(startCounting,1);
  42.     }
  43. </script>
  44. </head>
  45.  
  46. <body>
  47.     <input type="button" value="Send Object" onclick="startCounting()" />
  48.     <div id="val">    </div>
  49. </body>
  50. </html>
  51.  
  52.  
Reply
  #5  
Old September 6th, 2008, 12:07 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,964
Default

One solution is to make 'a' global. If you don't want to do that, you can use a closure.
Reply
  #6  
Old September 7th, 2008, 01:11 PM
rizwan6feb's Avatar
Member
 
Join Date: Jul 2007
Posts: 103
Default

Quote:
Originally Posted by acoder
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
Reply
  #7  
Old September 7th, 2008, 01:28 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,964
Default

Basically creating a function which returns a function, e.g.
Expand|Select|Wrap|Line Numbers
  1. function updateText2(a) {
  2.     return function() {
  3.         updateText(a);
  4.     }
  5. }
and calling that instead. See this link for an explanation of closures.
Reply
  #8  
Old September 8th, 2008, 04:10 AM
rizwan6feb's Avatar
Member
 
Join Date: Jul 2007
Posts: 103
Default

I have used closure, but the problem persists. Please see the code below what i am doing wrong
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="javascript">
  4.     var x=0;
  5.     function getAjaxObject(){
  6.         try{
  7.             ajaxRequest = new XMLHttpRequest();
  8.         } catch (e){
  9.             // Internet Explorer Browsers
  10.             try{
  11.                 ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  12.             } catch (e) {
  13.                 try{
  14.                     ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
  15.                 } catch (e){
  16.                     // Something went wrong
  17.                     alert("Your browser doesn't support ajax!");
  18.                     return false;
  19.                 }
  20.             }
  21.         }
  22.         return ajaxRequest;    
  23.     }
  24.     function updateText(a){
  25.         return function(){
  26.             if(a.readyState==4){
  27.                 var text=a.responseText;
  28.                 var e=document.getElementById('val');
  29.                 if(e){
  30.                     e.innerHTML=text;
  31.                 }
  32.             }
  33.         };
  34.     }
  35.     function startCounting(){
  36.         var a=getAjaxObject();
  37.         var fun1=updateText(a);
  38.         a.onreadystatechange = fun1;
  39.         a.open("GET", "incrementer.php", true);
  40.         a.send(null);
  41.         setTimeout(startCounting,1);
  42.     }
  43. </script>
  44. </head>
  45.  
  46. <body>
  47.     <input type="button" value="Start Counting" onclick="startCounting()" />
  48.     <div id="val">    </div>
  49. </body>
  50. </html>
  51.  
Reply
  #9  
Old September 8th, 2008, 06:37 AM
rnd me's Avatar
Expert
 
Join Date: Jun 2007
Location: Urbana IL
Posts: 387
Default

Expand|Select|Wrap|Line Numbers
  1.  
  2.     function startCounting(){
  3.         var a=getAjaxObject();
  4.         a.onreadystatechange = function(){
  5.             if(a.readyState==4){
  6.                 var text=a.responseText;
  7.                 var e=document.getElementById('val');
  8.                 if(e){
  9.                     e.innerHTML=text;
  10.                 }
  11.                 a = null;
  12.             }
  13.         }
  14.         a.open("GET", "incrementer.php", true);
  15.         a.send(null);
  16.         setTimeout(startCounting,1);
  17.     }
  18.  
Reply
  #10  
Old September 8th, 2008, 07:02 AM
rizwan6feb's Avatar
Member
 
Join Date: Jul 2007
Posts: 103
Default

Yes, that works. Great job man, thank you very much
Reply
  #11  
Old September 8th, 2008, 12:39 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,964
Default

So simple that I didn't think of. Thanks rnd me ;)
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.