Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8718339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:31:14+00:00 2026-06-13T06:31:14+00:00

Edit: I would be greatful if an experienced programmer with the ability to verify

  • 0

Edit:

I would be greatful if an experienced programmer with the ability to verify this sort of thing showed me the proof that this method is safe from memory leaks. I’ve been introducing it to many of my coding efforts but I still have a small doubt in my mind. Unfortunately I’m not good enough / don’t know the tools to investigate it.

Original:

I learned recently that some uses of lambda expressions can create memory leaks:

    ProjectData Project;

    void OnLaunchNewProject()
    {
        NewProjectUI newProjectUI = new NewProjectUI();
        newProjectUI.OnCompleted += (o, e) =>
            {
                Project = newProjectUI.NewProject;
                view.Content = string.Format("Project {0} was created and saved successfully.", Project.Name);
            };
        newProjectUI.OnCancelled += (o, e) => { view.Content = "Operation was cancelled.";};
        view.Content = newProjectUI;
    }

I learned the bad impact of this method in this blog.

I do not fully understand the impact of referencing local variables in lambda expressions and this limits my ability to circumvent the problem.

Between the typical approach and the use of lambda, what’s the ideal compromise? The thing I like about lambda is skipping the definition of the EventHandler’s arguments in the body of my class (the sender/routed args) when I don’t need them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:31:16+00:00Added an answer on June 13, 2026 at 6:31 am

    Unfortunately, the blog post you mentioned is wrong. There is not general issue with memory leaks in lambda expressions. In the blog examples, the finalizers are never called because the author never removes the anonymous methods from the event. So the .NET runtime thinks that the method still might be called later and cannot remove the class from memory.

    In your code, you will release the NewProjectUI instance somewhere, and this is the point where all events become unsued and when the assigned lambda method also becomes unused. Then the GC can remove the anonymous lambda-helper-class and free the used memory.

    So, again: There is no issue in .NET when using local variables in lambda expressions.

    But to make your code better, move the code from the lambda expressions to named methods and add these methods to the events. This also makes it possible to remove the methods from the events when they’re no longer needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

**EDIT: There are several options below that would work. Please vote/comment according to your
Edit: Appended or LDAP to question title to indicate that I would be fine
A VERY nice to have would be if I could edit object-literals in this
I would like to edit matlab scripts in two cases (1) In matlab Command
I would like to place an edit control (WC_EDIT) on a static control (WC_STATIC).
How would I change the below script to edit the original file instead of
I would like to know how I could edit mp3 files on the server
I would like to write a script which will edit multiple XML files, I
I would like the user to be able to edit the title of the
On my edit pages, how would I show the correct radio button if for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.