Re: Delegates and Events confusion

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: J.Marsch (jeremy_at_ctcdeveloper.com)
Date: 06/15/04


Date: Tue, 15 Jun 2004 18:21:05 -0500

Are you unsubscribing the event when you remove the instances of Class C
from memory?

If you subscribe to an event and do not unsubscribe, the subscriber will be
held in memory until the event publisher (class A) is collected.

Here's why:
A delegate has a member called Target. Target is a reference to the
subscriber (Class C). When you add the event, the publisher holds a
reference to the delegate which in turn holds a reference to the subscriber,
so C is held in memory. The graph looks like this:

A ---> EventDelegate ---->C

So, the reason that you are getting multiple event fires is that you have
more instances of C in memory than you think that you do.

What you probably want to do is to keep track of all of the event
subscriptions that C makes and have C implement IDispose. When you are
ready to kill C, call its Dispose method. Inside the Dispose, unsubscribe
from all of the events that you are subscribed to.

"Rhy Mednick" <rhy@rhy.com> wrote in message
news:uVScA1yUEHA.584@TK2MSFTNGP09.phx.gbl...
> I have a class (let's call it ClassA) that I've written which has events.
> In another class (let's call it ClassB) I create a collection of ClassA
> objects. In a third class (ClassC) I create a reference to some of the
> ClassA objects created in ClassB. In ClassC I hook into the ClassA events
> with a foreach loop so that I hook each object. The code is something
like
> this:
> class ClassC {
> void SomeMethod()
> {
> foreach (ClassA item in ClassACollection)
> {
> item.MyEvent += new EventHandler(item_MyEvent);
> }
> }
> }
>
> Objects of type ClassC keep getting created and deleted, but the objects
of
> ClassA that it references stay in memory. Therefore, every time I load a
> new instance of ClassC the event gets hooked again. What I'm seeing is
that
> I fire the event once but I end up getting it raised in the code that
> responds to it (item_MyEvent method) for every time it was added when I
only
> want it to get caught once.
>
> How can I be sure that I only add the handler to the event once for each
> item? Since the code is in ClassC it's not allowed to inspect the
> ClassA.MyEvent to see if something's already hooked to it.
>
> I hope this makes sense. Creating a repro case wouldn't make sense here
> because it's my application logic that's causing the problem.
>
> Thanks.
>
>



Relevant Pages

  • Re: Socket Async Send I/O - Resource Leak / Callbacks not getting called?
    ... I have a C# Application that reads a UDP broadcast. ... If I connect a subscriber, and start dispatching the incoming packets ... Suddenly the application memory consumption starts increasing... ... That means that the send actually didn't occur, this means that all managed and unmanaged resources associated with the logical connection stay allocated which leads to an increased Private bytes consumption. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Object lifetime/event problem
    ... When you destroy your subscriber object do you remove the event handler? ... objects now exist in memory, each one subscribing to the event. ...
    (microsoft.public.dotnet.general)