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 6123797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:03:31+00:00 2026-05-23T16:03:31+00:00

I use some UserControls which get created and destroyed within my application during runtime

  • 0

I use some UserControls which get created and destroyed within my application during runtime (by creating and closing subwindows with these controls inside).
It’s a WPF UserControl and inherits from System.Windows.Controls.UserControl. There is no Dispose() method I could override.
PPMM is a Singleton with the same lifetime as my application.
Now in the constructor of my (WPF) UserControl, I add an event handler:

public MyControl()
{
    InitializeComponent();

    // hook up to an event
    PPMM.FactorChanged += new ppmmEventHandler(PPMM_FactorChanged);
}

I got used to removing such event handler in the destructor:

~MyControl()
{
    // hook off of the event
    PPMM.FactorChanged -= new ppmmEventHandler(PPMM_FactorChanged);
}

Today I stumbled upon this and wondered:

1) Is this neccessary? Or does the GC take care of it?

2) Does this even work? Or would I have to store the newly created ppmmEventHandler?

I’m looking forward to your answers.

  • 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-23T16:03:32+00:00Added an answer on May 23, 2026 at 4:03 pm

    Since PPMM is a long-lived object (singleton), then this code doesn’t make much sense.

    The problem here is that as long as that event handler is referencing the object, it will not be eligible for garbage collection, as least as long as that other object that owns the event is alive.

    As such, putting anything in the destructor is pointless, as either:

    1. The event handler has already been removed, thus the object became eligible for garbage collection
    2. The event handler is not removed, the owning object is not eligible for garbage collection, and thus the finalizer will never get called
    3. Both objects are eligible for garbage collection, in which case you should not access that other object at all in the finalizer since you don’t know its internal state

    In short, don’t do this.

    Now, a different argument could be said about adding such code to the Dispose method, when you’re implementing IDisposable. In that case it fully makes sense since its usercode that is calling Dispose, at a predefined and controlled point.

    The finalizer (destructor), however, is only called when the object is eligible for garbage collection and has a finalizer, in which case there is no point.

    As for question nbr. 2, which I take as “Can I unsubscribe from events like that”, then yes, you can. The only time you need to hold on to the delegate you used to subscribe with is when you’re constructing the delegate around an anonymous method or a lambda expression. When you’re constructing it around an existing method, it will work.


    Edit: WPF. right, didn’t see that tag. Sorry, the rest of my answer doesn’t make much sense for WPF and since I am no WPF-guru, I can’t really say. However, there’s a way to fix this. It’s entirely legal here on SO to poach the content of another answer if you can improve it. So if anyone knows how to properly do this with a WPF usercontrol, you’re free to lift the entire first section of my answer and add the relevant bits of WPF.


    Edit: Let me respond to the question in the comment inside here as well.

    Since the class in question is a user-control, its lifetime will be tied to a form. When the form is closing, it will dispose of all child controls that it owns, in other words, there is already a Dispose method present here.

    The correct way for a user control to handle this, if it manages its own events, is to unhook the event handlers in the Dispose method.

    (rest removed)

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

Sidebar

Related Questions

I could use some help writing a regular expression. In my Django application, users
I could use some help creating an XSL template that will take a string
I have a C# class question which I hope to get some help with.
I am trying to use MEF on a new project within my application. Things
I have an app which uses some usercontrols. I want to take a thumbnail
I am currently developing some user controls so that I can use them at
I'm looking at building some web user controls with an eye toward re-use, but
I use some HTTP Handlers from my Web Forms days forms, to for example
I use some CSS styling to hide input buttton, images and so on but
i use some code for inline cell editing from Apples TaggedLocations Example and im

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.