Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to remove the handler

the more I dig into C# and the GC I find more and more things I'm not quite sure about. I always thought that the Dispose and the respective Finalizer is only necessary when having some unmanaged resources in my classes.

But I have many occasions where there are only native C# classes where it is not quite clear to me if I need the Dispose and the respective Finalizer. For instance when I have event-handlers attached to my events.

Do I need to remove the event-handlers when I call Dispose. Also it was told to me that the object might not get collected if event-handlers are still attached. If this is the case the GC is somehow compromised.

Is it possible to summarize when and how I need to implement Dispose and Finalizer?

In fact I have more questions about this, but maybe an answer to this question could help me further.

like image 851
msedi Avatar asked Mar 16 '15 18:03

msedi


People also ask

Do I need to remove event handlers c#?

Yes, you will want to explicitly remove the event handlers. Just because the object is out of scope of its original allocation does not mean it is a candidate for GC. As long as a live reference remains, it is live.

How do event handlers work?

Each key node has a handler that receives key events when the key has focus. The handler responds to the key-pressed and key-released events for the Enter key by changing the color of the key on the screen. The event is then consumed so that the keyboard node, which is the parent node, does not receive the event.

Which function is used to remove an existing event handler?

The removeEventListener() method removes an event handler from an element.


1 Answers

To clear up your general question of when to do Dispose and Finalize:

If you have a field in your class that is a IntPtr (or some other kind of unmanaged resource, but a IntPtr is the most common) and it is your classes responsibility to clean up that resource then you need to implement a finalizer. In that finalizer you should deallocate whatever resource the IntPtr points too. If you don't have a IntPtr then the class you are holding on to should be handling its own finalization and would implement IDisposeable (see the next part)

If you have a field in your class that implements IDisposable and your class is responsible for cleaning up after that object your class should also implement IDisposable and in that dispose method you should call Dispose() on the object.

For event handlers, it is a matter of personal preference. You can, but it only matters if you do or not if the person who subscribed to the event messed up their code.

Unless you expect the publisher of the event to outlive the subscriber, there's no reason to remove the event handler...

I personally do not, but I do know some people who do. If you wanted to do it, the process is simply setting the event handler to null in your dispose method.

public sealed class Example : IDisposable 
{
    public EventHandler MyEvent;

    public void Dispose()
    {
        MyEvent = null;
    }
}

EDIT: And a good point that Hans Passant brought up in the comments: You never need a finalizer, if you have a unmanaged resource that would need a finalizer it should get wrapped up in a SafeHandle wrapper to handle the finalization for you. Once you do that the object just becomes another normal IDisposable you need to take care of in your .Dispose() method.

like image 53
Scott Chamberlain Avatar answered Oct 23 '22 03:10

Scott Chamberlain