为了防止内存泄漏在ActionScript 3.0中,我使用的成员向量中具有与矢量工作类,例如:在这种情况下如何避免内存泄漏?
public class A
{
private var mHelperPointVector:Vector.<Point> = new Vector.<Point>();
public static GetFirstData():Vector.<Point>
{
mHelperPointVector.length = 0;
....
return mHelperPointVector;
}
public static GetSecondData():Vector.<Point>
{
mHelperPointVector.length = 0;
....
return mHelperPointVector;
}
}
,然后我有消费者谁使用GetFirstData和GetSecondData方法,存储的引用由这些方法所返回的载体,例如:
public function OnEnterFrame():void
{
var vector:Vector.<Point> = A.GetSecondData();
....
}
这招似乎是良好的,但有时我需要处理的一段时间后通过GetSecondData()返回的向量,并且在这种情况下,该矢量被另一个调用GetSecondData()或GetFirstData()...覆盖的解决方案就是将矢量复制到一个新的矢量中......但在这种情况下更好地避免这个技巧。你如何处理这些问题?我必须使用大量的矢量(每个长度在1-10之间)。
大小为10的两个向量不应该是一个问题。我想象分配/创建新向量并填充值的问题,但希望看到更多的代码(需要测试的基本工作示例)才能确定。作为一个想法,当你做GetFirstData时,你的类A可以有一个布尔标志来检查它是否应该清除以前的数据。另外,还有一些脱离主题,请看Object Pools。 –