代表们会导致内存泄漏吗?
我的意思是,例如如果一个类A
包含一个ADelegate
,并且后者指向BMethod
(属于B
类),这可以防止GC收集A类或B类吗?
如果是这样,我们如何“释放”代表(设置ADeletate = Nothing
/null?)
您如何评价这一点:
//Class A Finalize, containing ADelegateInstance as ADelegate'
protected override void Finalize()
{
ADelegateInstance =
(ADelegate)System.Delegate.RemoveAll(
ADelegateInstance, ADelegateInstance);
ADelegateInstance = null;
base.Finalize();
}
'Class A Finalize, containing ADelegateInstance as ADelegate'
Protected Overrides Sub Finalize()
ADelegateInstance = _
CType(System.Delegate.RemoveAll(ADelegateInstance, ADelegateInstance), _
ADelegate)
ADelegateInstance = Nothing
MyBase.Finalize()
End Sub
最佳答案
是的,除非您取消订阅该 Activity ,否则引用将保持 Activity 状态:
someObject.SomeEvent -= SomeDelegate;
关于.net - 代表们会导致内存泄漏吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2227762/