Solved

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

Posted on 2013-12-19
6
833 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
DevOps Toolchain Recommendations

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

 

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

Suggested Solutions

Title # Comments Views Activity
Simple Injector with Web Service 4 41
VB.NET 2008 Publish Error 2 27
Send SMS from VB via Sierra Wireless Modem 2 21
JSON  parse help 7 25
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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