2

I was just wondering if you need to unregister events before creating a new instance of an object... for clarity:

Say I have a Hyperlink and have already created an instance for it.

Hyperlink m_myHyperlink = new Hyperlink();

In the code where the control sets the hyperlink up there are events registered to it, based off certain conditions.

...

private void SetValue(...)
{
    ...

    m_myHyperlink = new Hyperlink(new Run(displayText));

    Uri path = null;

    if (Uri.TryCreate(link, UriKind.Absolute, out path)
    {
         m_myHyperlink.NavigateUri = path;
         m_myHyperlink.RequestNavigate += new RequestNavigateEventHandler(foo);
    }
    else
    {
         m_myHyperlink.Click += new RoutedEventHandler(bar);
    }
}

...

So, the next time the value is set, a new instance is created for m_myHyperlink and events are registered based on the conditions.

Do, I need to worry about unregistering the events before the new instance is created? So, the code would look something like this.

...

private void SetValue(...)
{
    ...

    // I know one of these would not be registered, but it is legal to attempt to
    // unregister it anyways...
    m_myHyperlink.RequestNavigate -= new RequestNavigateEventHandler(foo);
    m_myHyperlink.Click -= new RoutedEventHandler(bar);

    m_myHyperlink = new Hyperlink(new Run(displayText));

    Uri path = null;

    if (Uri.TryCreate(link, UriKind.Absolute, out path)
    {
         m_myHyperlink.NavigateUri = path;
         m_myHyperlink.RequestNavigate += new RequestNavigateEventHandler(foo);
    }
    else
    {
         m_myHyperlink.Click += new RoutedEventHandler(bar);
    }
}

...

I'm guessing that the GC can dispose of the old instance just fine without unregistering the events, but wanted to make sure and see if there is any benefit to help the GC of the old instance by unregistering the events before creating the new instance.

MikeDev
  • 21
  • 3
  • The .NET garbage collector is very smart. You almost never have to do anything to help it out, unless you're working with `unsafe` code or external resources. *Edit:* However, looking at the other question, this might, in fact, be one of those times... – Bobson May 19 '14 at 17:20
  • 1
    This will give you a idea: http://stackoverflow.com/questions/298261/do-event-handlers-stop-garbage-collection-from-occuring – Sriram Sakthivel May 19 '14 at 17:25
  • I'm actually surprised by the answer on the other question. I never deattach my event handlers from objects. I guess I learn something new every day. – Icemanind May 19 '14 at 17:27
  • @icemanind You need to remove event handlers if any objects used *in the event handler* should be collected. If the object *defining the event* needs to go away there's no need to remove any event handlers. – Servy May 19 '14 at 17:30
  • 2
    Mike, setting a new `Hyperlink` object to that instance field doesn't actually remove the previous control, nor does it create a new control, so this code won't *work*, regardless of whether it's leaking memory. You also can't really leak memory in an ASP app like this, because the entire page and everything in it is all torn down at the end of the request. It's not like a desktop application where these types of objects persist over time. When the entire UI is torn down milliseconds after being created every time, you don't really need to worry about leaks, other than through `Session`. – Servy May 19 '14 at 17:32
  • @Servy - Not entirely true. My first attempt at a MVC website leaked memory like crazy because we weren't closing our database connections. We weren't *saving* them, but we weren't disposing them either. – Bobson May 19 '14 at 17:57
  • @Bobson Sure, that's an unmanaged resource. The whole point of an unmanaged resource is that the GC won't manage it. – Servy May 19 '14 at 18:03
  • @Servy - Entirely true - I just figured it was worth pointing out that being an ASP page doesn't make it immune to unmanaged leaks, just the managed ones. I just wasn't very explicit about the larger picture in my example. – Bobson May 19 '14 at 18:06
  • Thanks for the feedback. This is part of a desktop app. To give some context, a prop sheet is built to display values for a list of items. So, when you select another item in the list the property sheet is reused and the values are updated. One of the controls the prop sheet houses holds the hyperlink indicated above and a new instance of the hyperlink is created based off the selected item. Once the list is navigated away from the prop sheet and its controls are disposed of. I was just curious if creating the new hyperlink instance opened the old instance up to the GC without unregistering. – MikeDev May 19 '14 at 18:40

0 Answers0