gpt4 book ai didi

.net - 委托(delegate)们会导致内存泄漏吗?

转载 作者:行者123 更新时间:2023-12-04 16:41:43 33 4
gpt4 key购买 nike

代表们会导致内存泄漏吗?

我的意思是,例如如果一个类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 - 委托(delegate)们会导致内存泄漏吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2227762/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com