dominicwong
asked on
Will Action/Func causes memory leak when stored as member reference inside another class?
Hi experts
I am passing a method into another class which stores this method as a member reference and Property method. Will these cause memory leak?
Thanks.
I am passing a method into another class which stores this method as a member reference and Property method. Will these cause memory leak?
public class KeepReference
{
private Action _callBackMethod1;
public KeepReference(Action aMethod)
{
_callBackMethod1 = aMethod;
}
public Action CallBackMethod2 {get; set;}
}
public class CallingClass
{
public void abc()
{
:
KeepReference kkk = new KeepReference(CallBackMethod); // memory leak?
:
kkk.CallBackMethod2 = CallBackMethod; // memory leak?
}
private void CallBackMethod()
{...}
}
Thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What is your idea of a "memory leak?"
ASKER
The memory leak that I am concerned about is that it is not released by garbage collector.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks anarki_jimbel and kaufmed for your prompt responses.
ASKER
But for normal delegates using "+=", the caller has to explicitly call "-=" in order to avoid memory leak. In my case, I use the delegate "Action" and that makes me ponder if I need to do similar thing in order to avoid memory leak.