Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-12-19
6
Medium Priority
?
988 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 1000 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

636 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