2011-09-19 54 views
0

我写道:内存泄漏在一个包裹的XMLHttpRequest功能

function ao(){ 
this.count=0; 
this.flag=0; 
this.tmr=0; 
var self = this; 
this.make=function(){ 
    //log("before: "+this.url+" "+this.xhr); 
    self.xhr = (window.XMLHttpRequest) 
     ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); 
    //log("after: "+this.xhr); 
} 
this.request = function (method, url, sendStr, delay){ 
    this.delay=delay; 
    if(delay && self.tmr==0){ 
     self.start(); 
    } 
    if(self.flag==0){ 
     this.method = method; 
     this.url = url; 
     this.sendStr = sendStr; 
     self.make(); 
     this.xhr.open(method, url, true); 
     this.xhr.onreadystatechange = this.stateChange; 
     this.xhr.onabort=this.rrr; 
     this.xhr.onerror=this.rrr; 
     this.xhr.setRequestHeader("Cache-Control","no-cache"); 
     this.xhr.send(sendStr); 
    } 
}; 
this.repeat=function(){ 
    if(this.flag==0){ 
     this.flag=1; 
     this.count++; 
     this.xhr.open(self.method, self.url+"?"+this.count, true); 

     this.xhr.onreadystatechange = this.stateChange; 
     this.xhr.onabort=this.rrr; 
     this.xhr.onerror=this.rrr; 
     this.xhr.setRequestHeader("Cache-Control","no-cache"); 

     this.xhr.send(self.sendStr); 
    } 
    return 0; 
} 
this.stop=function(){ 
    window.clearInterval(this.tmr); 
    this.tmr=0; 
    this.flag=0; 
} 
this.start =function(){ 
    self.tmr=window.setInterval(function(){self.repeat();},self.delay); 
} 
this.stateChange = function(){ 
    if (self.xhr.readyState <= 1){ 
     return; 
     self.log("404 errors"); 
    } else { 
     if (self.xhr.readyState == 4 && self.xhr.status == 200){ 
      self.resp = self.xhr.responseText; 
      if (self.callback != null) 
       self.callback(self.xhr.readyState, self.xhr.status); 
      else { 
       if (self.getHTML) { 
        self.getHTML(self.resp); 
        this.xhr=null; 
       } else { 
        if (self.xhr.readyState == 4 && self.xhr.status == 200){ 
         self.parseJSON(); 
         self.traverse(); 
         this.ro=null; 
         this.xhr=null; 
        } 
       } 
      } 
     } 
    } 
    self.flag=0; 
    return 0; 
}; 

和窗户FF有内存泄漏。我花了好几天的时间来修复它,但我很难过。

以下工作:

var x=new ao(); 
ao.request("POST","/cgi-bin/sdf.cgi","text",1000) 

,每1000毫秒,如果前一个请求完成后,它使新的要求。

+0

那么是什么问题? –

+0

它在Firefox中有内存泄漏 – request

+0

你是如何确定它泄漏内存的? –

回答

3

当涉及到使用XMLHttpRequest对象的onreadystatechanged事件时,开发人员还应该采取预防措施。如果处理程序 是关闭对同一个XMLHttpRequest对象的引用的闭包,则可以创建另一个循环依赖关系。这不是 必需由上述工具检测到,因为该对象不是DOM的部分 。 Link