Solved

Will Action/Func causes memory leak when stored as member reference inside another class?

Posted on 2013-12-19
6
848 Views
Last Modified: 2013-12-19
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?

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()
      {...}
}

Open in new window


Thanks.
0
Comment
Question by:dominicwong
  • 3
  • 2
6 Comments
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 250 total points
ID: 39730983
Is it causing?

For me it looks pretty OK. However, referenced class won't be garbage collected unless you either:

 - destroy the current object
 - destroy the reference (set to null)
0
 

Author Comment

by:dominicwong
ID: 39731003
Not sure whether it is leaking memory.
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.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39731045
What is your idea of a "memory leak?"
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:dominicwong
ID: 39731053
The memory leak that I am concerned about is that it is not released by garbage collector.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 39731062
Then I agree with anarki_jimbel, though in this case all the references are destroyed once abc returns (since you have declared everything local to the method).
0
 

Author Closing Comment

by:dominicwong
ID: 39731067
Thanks anarki_jimbel and kaufmed for your prompt responses.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question