Re: Delegates and Events confusion
From: J.Marsch (jeremy_at_ctcdeveloper.com)
Date: 06/15/04
- Next message: Bill Jones: "Re: Debug Win32 DLL from C# Client"
- Previous message: Rhy Mednick: "Delegates and Events confusion"
- In reply to: Rhy Mednick: "Delegates and Events confusion"
- Next in thread: Rhy Mednick: "Re: Delegates and Events confusion"
- Reply: Rhy Mednick: "Re: Delegates and Events confusion"
- Reply: Angelos Petropoulos: "Re: Delegates and Events confusion"
- Messages sorted by: [ date ] [ thread ]
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.
>
>
- Next message: Bill Jones: "Re: Debug Win32 DLL from C# Client"
- Previous message: Rhy Mednick: "Delegates and Events confusion"
- In reply to: Rhy Mednick: "Delegates and Events confusion"
- Next in thread: Rhy Mednick: "Re: Delegates and Events confusion"
- Reply: Rhy Mednick: "Re: Delegates and Events confusion"
- Reply: Angelos Petropoulos: "Re: Delegates and Events confusion"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|