MagicAjax中的内存泄漏

之前用 MagicAjax 做了一个项目,已经上线了两三个月,最近客户说它经常会把 IE 弄死,找了好几天,没有发现程序有什么问题,但是浏览器偶尔是会死掉(占 CPU 50%-双核),程序都找完了就去找 MagicAjax 的代码,意外发现了 MagicAjax 的 JS 中有一个内存泄漏的 BUG (不过和我的问题无关,因为我改了后, IE 还是会死),现在说一下这个 BUG 供使用 MagicAjax 的朋友参考一下.
在 AjaxCallObject 类的方法 DoAjaxCall 有这样一段代码:

Code
-->  1  if this .XmlHttp )
 2      {        
 3           if  (waitElement)
 4          {
 5              waitElement.style.visibility  =   ' visible ' ;
 6              MoveWaitElement();
 7          }
 8 
 9           var  oThis  =   this ;
10          __AJAXCboList.push(oThis);
11          AJAXCbo  =   new  AjaxCallObject();
12          
13           if  (__bTracing)
14          {
15               this .CreateTracingWindow();
16               this .TraceSentData(theData);        
17          }
18 
19           if this .XmlHttp.readyState  ==   4   ||   this .XmlHttp.readyState  ==   0  )
20          {
21               if  (  !  ajaxCallType  ||  ajaxCallType.toLowerCase()  !=   " sync " )
22              {
23                   //  Asynchronous
24                   this .XmlHttp.open( " POST " , thePage,  true );
25                   this .XmlHttp.onreadystatechange  =   function (){ oThis.ReadyStateChange(); };
26                   this .XmlHttp.setRequestHeader( ' Content-Type ' ' application/x-www-form-urlencoded ' );
27                   this .XmlHttp.send(theData);
28              }
29               else
30              {
31                   //  Synchronous
32                   //  Use a timeout so that the screen refreshes before getting stack waiting the AjaxCall.
33                  window.setTimeout(
34                       function ()
35                      {
36                          oThis.XmlHttp.open( " POST " , thePage,  false );
37                          oThis.XmlHttp.setRequestHeader( ' Content-Type ' ' application/x-www-form-urlencoded ' );
38                          oThis.XmlHttp.send(theData);
39 
40                           if ( oThis.XmlHttp.status  ==   200   &&  oThis.XmlHttp.statusText  ==   " OK "  )
41                              oThis.OnComplete(oThis.XmlHttp.responseText, oThis.XmlHttp.responseXML);
42                           else
43                              oThis.OnError(oThis.XmlHttp.status, oThis.XmlHttp.statusText, oThis.XmlHttp.responseText);
44                      },  1 );
45              }            
46          }
47      }

其中 __AJAXCboList 是一个数组,在每次提交 ( POST ) 数据前会把当前对象放入数组中,并新建一个对象供下一个提交( POST )使用.当在使用异步提交时,处理服务器返回数据的方法如下 :

Code

可见当提交( POST )完成后,会给之前放入的 AJAXCbo 对象赋空值并从数组 __AJAXCboList 中删除,以便浏览器回收对象,这里没有什么问题,有问题的是当使用同步提交( POST )时, MagicAjsx 并没有使用 ReadyStateChange 方法来处理回发的数据,而是用直接写的方法来处理的:

 1  //  Synchronous
 2  //  使用定时器,以便显示那个黑黑的Loading层,可见MagicAjax中的同步不是真正的同步
 3  window.setTimeout(
 4       function ()
 5      {
 6          oThis.XmlHttp.open( " POST " , thePage,  false );
 7          oThis.XmlHttp.setRequestHeader( ' Content-Type ' ' application/x-www-form-urlencoded ' );
 8          oThis.XmlHttp.send(theData);
 9 
10           if ( oThis.XmlHttp.status  ==   200   &&  oThis.XmlHttp.statusText  ==   " OK "  )
11              oThis.OnComplete(oThis.XmlHttp.responseText, oThis.XmlHttp.responseXML);
12           else
13              oThis.OnError(oThis.XmlHttp.status, oThis.XmlHttp.statusText, oThis.XmlHttp.responseText);
14      },  1 );

当提交( POST )完成后,__AJAXCboList数组中将始终保留这个 AJAXCbo 对象.我测试过,没有其它的地方会释放里面的数据,除非刷新页面.我后来把它改成也用 ReadyStateChange 方法来处理就没有这个问题了.

 1  window.setTimeout(
 2  function ()
 3  {
 4      oThis.XmlHttp.open( " POST " , thePage,  false );
 5      oThis.XmlHttp.onreadystatechange  =   function (){ oThis.ReadyStateChange(); };
 6      oThis.XmlHttp.setRequestHeader( ' Content-Type ' ' application/x-www-form-urlencoded ' );
 7      oThis.XmlHttp.send(theData);
 8 
 9       // if( oThis.XmlHttp.status == 200 && oThis.XmlHttp.statusText == "OK" )
10       //     oThis.OnComplete(oThis.XmlHttp.responseText, oThis.XmlHttp.responseXML);
11       // else
12       //     oThis.OnError(oThis.XmlHttp.status, oThis.XmlHttp.statusText, oThis.XmlHttp.responseText);
13  },  1 );
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值