Solved

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

Posted on 2013-12-19
6
776 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 74

Expert Comment

by:käµfm³d 👽
ID: 39731045
What is your idea of a "memory leak?"
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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 74

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now