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

  • Home
  • SEARCH
  • 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 932389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:39:38+00:00 2026-05-15T20:39:38+00:00

Can you see downsides to this one-liner other than the fact that multiple uses

  • 0

Can you see downsides to this one-liner other than the fact that multiple uses of it would violate the DRY principle? It seems straightforward but the fact that I haven’t seen others propose it makes me wonder if there’s a downside to it.

This bit of code creates a WeakReference to a method and then registers an event handler that invokes the reference’s target.

SomeEvent += (sender, e) => ((Action)(new WeakReference((Action)ProcessEvent)).Target)();

Thanks,
Ben

  • 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-05-15T20:39:39+00:00Added an answer on May 15, 2026 at 8:39 pm

    I don’t think that pattern does what you expect. Are you trying to prevent the event from holding a reference to the current object so as to prevent memory leaks? The lambda expression will capture the value of this in order to evaluate ProcessEvent (assuming ProcessEvent is an instance method), so you will still have the leak. This code is the same as doing SomeEvent += (sender, e) => ProcessEvent();.

    You may be trying to do something more like this (which also isn’t what you want):

    var reference = new WeakReference((Action)ProcessEvent);
    SomeEvent += (sender, e) => ((Action)reference.Target)();
    

    Now the lambda expression will capture the WeakReference, so you won’t have a strong reference to this. Unfortunately, nothing else is referencing the delegate created from ProcessEvent, so it will be removed on the next GC even if this is still alive. (This also doesn’t check for Target being null).

    You could try something like this:

    public EventHandler MakeWeakHandler(Action action, Action<EventHandler> remove)
    {
        var reference = new WeakReference(action.Target);
        var method = action.Method;
        EventHandler handler = null;
        handler = delegate(object sender, EventArgs e)
        {
            var target = reference.Target;
            if (target != null)
            {
                method.Invoke(target, null);
            }
            else
            {
                remove(handler);
            }
        };
        return handler;
    }
    

    and then use it like this:

    SomeEvent += MakeWeakHandler(ProcessEvent, h => SomeEvent -= h);
    

    That will keep a weak reference to the receiver of ProcessEvent, and will automatically remove the event handler from the event after it has been collected, which should prevent memory leaks as long as the event is raised regularly.

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

Sidebar

Related Questions

we can see function generate() { this.a = 'hello'; } obj = new generate();
I can see this topic has been brought up before but not answered successfully.
I can see people asking all the time whether multiple inheritance should be included
I can see in the postgresql logs that certain simple queries (no joins and
I can see there are a few options for doing this. My question is
This is one thing that has been bugging me for a while about DDD.
You can see what I'm talking about in the screenshot below. Basically, I have
We can see in a directory files ordered by Name in Windows Explorer. If
I can see line numbers in my error logs in our development environment, in
I can see some options available: 1) Use PEAR's POP3 class --> tried it,

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.