前言
C/C++的开发,内存的管理是十分重要的课题,分配内存使用后要及时释放,否则内存泄漏导致内存耗尽。进入托管环境后,内存能够自动管理和回收,已经不用开发者太操心内存管理方面的问题了,在托管环境,开发者仍然可以做一些工作,提供内存回收的效率,例如,把类内部的引用对象设置为class="tags" href="/tags/NULL.html" title=null>null是最基本的。借鉴在.net环境下的IDispose模式,现在,在Flex/AS3里实现。
IDispose接口
首先定义控制资源释放的接口。
package cwn.core
{
/*
* 释放资源接口
*/
public interface IDispose
{
class="tags" href="/tags/FUNCTION.html" title=function>function Dispose():void;
}
}
实现IDispose接口
public class ClassA implements cwn.core.IDispose
{
private var _RefObject:Object;//引用高级对象
public class="tags" href="/tags/FUNCTION.html" title=function>function ClassA()
{
}
private var _Disposed:Boolean = false;//释放控制标志
protected class="tags" href="/tags/FUNCTION.html" title=function>function Disposing():void
{
}
public class="tags" href="/tags/FUNCTION.html" title=function>function Dispose():void
{
if (_Disposed)
return;
Disposing();//释放派生类的资源
_RefObject = class="tags" href="/tags/NULL.html" title=null>null; //释放ClassA资源
}
}
Disposing方法是派生类释放资源时按需要重写的,例如下面的ClassB
public class ClassB extends ClassA
{
private var _RefObject2:Object;
public class="tags" href="/tags/FUNCTION.html" title=function>function ClassB()
{
super();
}
override protected class="tags" href="/tags/FUNCTION.html" title=function>function Disposing():void
{
//释放ClassB资源
if(_RefObject2 is IDispose)
{
IDispose(_RefObject2).Dispose();
}
_RefObject2 = class="tags" href="/tags/NULL.html" title=null>null;
super.Disposing();
}
}
DisposeUtil对象
定义一个资源释放的工具类,方便调用,集中释放。
package cwn.core
{
class="tags" href="/tags/IMPORT.html" title=import>import flash.display.Bitmap;
class="tags" href="/tags/IMPORT.html" title=import>import flash.display.DisplayObject;
class="tags" href="/tags/IMPORT.html" title=import>import flash.display.MovieClip;
class="tags" href="/tags/IMPORT.html" title=import>import flash.display.Sprite;
class="tags" href="/tags/IMPORT.html" title=import>import flash.utils.Dictionary;
class="tags" href="/tags/IMPORT.html" title=import>import mx.collections.ArrayCollection;
class="tags" href="/tags/IMPORT.html" title=import>import mx.controls.SWFLoader;
public final class DisposeUtil
{
public static class="tags" href="/tags/FUNCTION.html" title=function>function Dispose(value:Object):void
{
if (value == class="tags" href="/tags/NULL.html" title=null>null)
return;
if (value is Array)
{
ClearArray(value as Array);
}
else if (value is ArrayCollection)
{
ClearArrayCollection(value as ArrayCollection);
}
else if (value is SWFLoader)
{
DisposeSWFLoader(value as SWFLoader);
}
else if (value is Sprite)
{
DisposeSprite(value as Sprite);
}
else if (value is Dictionary)
{
ClearDictionary(value as Dictionary);
}
}
private static class="tags" href="/tags/FUNCTION.html" title=function>function ClearArray(value:Array):void
{
if (value == class="tags" href="/tags/NULL.html" title=null>null)
return;
while (value.length > 0)
{
var child:IDispose = value.pop() as IDispose;
if (child != class="tags" href="/tags/NULL.html" title=null>null)
child.Dispose();
}
}
private static class="tags" href="/tags/FUNCTION.html" title=function>function ClearArrayCollection(value:ArrayCollection):void
{
if (value == class="tags" href="/tags/NULL.html" title=null>null)
return;
while (value.length > 0)
{
var child:IDispose = value.removeItemAt(0) as IDispose;
if (child != class="tags" href="/tags/NULL.html" title=null>null)
child.Dispose();
}
}
private static class="tags" href="/tags/FUNCTION.html" title=function>function DisposeSprite(value:Sprite):void
{
if (value == class="tags" href="/tags/NULL.html" title=null>null)
return;
while (value.numChildren > 0)
{
var child:DisplayObject = value.removeChildAt(0);
if (child is IDispose)
IDispose(child).Dispose();
}
}
private static class="tags" href="/tags/FUNCTION.html" title=function>function DisposeSWFLoader(value:SWFLoader):void
{
if (value == class="tags" href="/tags/NULL.html" title=null>null)
return;
if (value.content is Bitmap)
{
Bitmap(value.content).bitmapData.dispose();
}
else if (value.content is MovieClip)
{
MovieClip(value.content).stop();
}
value.unloadAndStop(true);//flash player 10后支持
value.source = class="tags" href="/tags/NULL.html" title=null>null;
}
private static class="tags" href="/tags/FUNCTION.html" title=function>function ClearDictionary(value:Dictionary):void
{
if (value == class="tags" href="/tags/NULL.html" title=null>null)
return;
for each (var key:* in value)
{
var val:IDispose = value[key] as IDispose;
if (value != class="tags" href="/tags/NULL.html" title=null>null)
val.Dispose();
delete value[key];
}
}
}
}