包装的 XMLHttpRequest 函数中的内存泄漏

发布于 2024-12-05 02:23:54 字数 2530 浏览 1 评论 0原文

我写了以下内容:

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;
};

在 Windows ff 中存在内存泄漏。我花了几天时间试图修复它,但我被难住了。

以下工作原理:

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

如果之前的请求完成,则每 1000 毫秒后,它会发出新的请求。

I wrote the following :

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;
};

and in windows ff there is a memory leak. I spent days trying to fix it, but I'm stumped.

The following works :

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

and after every 1000 miliseconds if previous request is done, it makes new request.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往事随风而去 2024-12-12 02:23:54

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


Developers should also take precautions when it comes to using the
onreadystatechanged event of an XMLHttpRequest object. If the handler
is a closure that closes over a reference to the same XMLHttpRequest
object, another circular dependency can be created. This isn't
necessairly detected by the above tool because the object is not part
of the DOM.
Link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文