Actionscript 3 清洁阵列集合
最初,我在flex模块中声明了十个ArrayCollection,我认为这会导致内存泄漏。所以我将它们分离在一个类中,我将使用我在其中创建的“destroy”方法清理它们。这样行吗 对不起,我讨厌这个题目。但我不会把它写成“引诱垃圾收集者”Actionscript 3 清洁阵列集合,actionscript-3,Actionscript 3,最初,我在flex模块中声明了十个ArrayCollection,我认为这会导致内存泄漏。所以我将它们分离在一个类中,我将使用我在其中创建的“destroy”方法清理它们。这样行吗 对不起,我讨厌这个题目。但我不会把它写成“引诱垃圾收集者” 如果对象没有附加侦听器,那么它应该执行null操作 cfds = null; 我从未在类本身上使用过[Bindable],因此不确定您在那里尝试做什么 package{ public final class Cfd{ priva
如果对象没有附加侦听器,那么它应该执行null操作
cfds = null;
我从未在类本身上使用过[Bindable],因此不确定您在那里尝试做什么
package{
public final class Cfd{
private static var instance;
private var _cfds:ArrayCollection = new ArrayCollection();
public function Cfd(singletonEnforcer:MySingletonEnforcer){
if (instance) { throw new Error('Cannot create a new instance. Must use Cfd.getInstance().') }
}
public static function getInstance():Cfd{
if (instance == null)
instance = new MySingleton(new MySingletonEnforcer());
return instance;
}
// don't see a real need for setter/getters here
public static function get cfds():ArrayCollection{
return _cfds;
}
public static function set cfds(value:ArrayCollection):void{
_cfds = value;
}
// I wouldn't even use a destroy on this class since it is a singleton.
// just set the data to null Cfd.cfds = null
public static function destroy():void{
_cfds = null
}
}
}
//this is in Cfd.as but is outside the package block
class MySingletonEnforcer {}
每当您使用这样的单例时,您几乎可以保证内存泄漏,因为您可能正在到处收听ArrayCollection(可能还有其中的项)。当通过getter/setter对显式提供对对象的引用时,可以在setter中添加侦听器,并在重置值时将其移除 查看更多正在发生的事情
有关为什么应该避免单例的详细信息,请参见类级别的
[Bindable]
使类的所有公共属性都可绑定。在这种特殊情况下,它与将它放在cfds
属性上完全相同,因为类中只有一个公共属性。我必须将它给你,以警告我有关单身的问题。非常感谢。
package{
public final class Cfd{
private static var instance;
private var _cfds:ArrayCollection = new ArrayCollection();
public function Cfd(singletonEnforcer:MySingletonEnforcer){
if (instance) { throw new Error('Cannot create a new instance. Must use Cfd.getInstance().') }
}
public static function getInstance():Cfd{
if (instance == null)
instance = new MySingleton(new MySingletonEnforcer());
return instance;
}
// don't see a real need for setter/getters here
public static function get cfds():ArrayCollection{
return _cfds;
}
public static function set cfds(value:ArrayCollection):void{
_cfds = value;
}
// I wouldn't even use a destroy on this class since it is a singleton.
// just set the data to null Cfd.cfds = null
public static function destroy():void{
_cfds = null
}
}
}
//this is in Cfd.as but is outside the package block
class MySingletonEnforcer {}