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

标签 javascript memory-leaks 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;
};

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

以下作品:

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

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

最佳答案

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

关于javascript - 包装的 XMLHttpRequest 函数中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7469749/

相关文章:

javascript - 在 Bower 中指定版本号

javascript - body 保持定位时水平滚动固定的 div

C++析构函数内存泄漏

java - android中java.lang.OutOfMemoryError的主要原因是什么

javascript - Puppeteer 在返回 HTML 之前不触发点击

javascript - 如何在 android 浏览器中捕获退格键代码 - javascript?

javascript - 如何避免 RegExp 中的灾难性回溯?

iphone - MiniZip 泄漏

javascript - Google Closure 编译器提示 XMLHttpRequest.response 类型

node.js - 否 'Access-Control-Allow-Origin'