> Matt,
>
[quoted text clipped - 6 lines]
> you maintain a reference to the object, the anonymous delegate will hang
> around.
And, as long as the anonymous delegate is hanging around, the object
that instantiated the anonymous delegate would be around as well.
It seems as if the potential for a leak could be fairly large in some
cases. This would be
one example:
- the object (B) that is attaching to the event holds a reference (the
only reference) to a
significant chunk of memory
- the object (A) that sources the event lives longer than object (B)
In this case, object B would be kept in memory by a single function's
anonymous delegate.
> The leak normally comes into play, when you have the reverse condition. You
> attach an event handler to an object, then dereference the object, while
> maintaining a link to the handler (the anonymous delegate above). Until you
> get ride of the handler object, the original object will hang around.
This is incorrect. Subscribing to another object's event does not
constitute a strong reference from the *event sink* to the *event
source*. The *event source* can disappear at any point. The *event
sink* cannot disappear without the *event source* first being disposed
of. Delegates do not keep track of which invocation lists they are part
of.
>>Now, after I've made my Process() method call, I actually have no way to
>>remove my anonymous delegate (without called GetInvocationList())!
[quoted text clipped - 7 lines]
> IMO, if you want the handler to only be active during the call to Process,
> then the anonymous delegate is the wrong tool to use.
There can be cases where you only want to subscribe to events for a
certain amount of time. The alternatives involve a lot of extra code.
> Hope this helps
> Jay
[quoted text clipped - 43 lines]
>>
>>Matt.
Jay B. Harlow [MVP - Outlook] - 04 Sep 2003 21:22 GMT
Matt,
> And, as long as the anonymous delegate is hanging around, the object
> that instantiated the anonymous delegate would be around as well.
No it wouldn't!
If the only thing referencing the object is the delegate and the only thing
referencing the delegate is the object. You have a circular reference, the
GC will get ride of both of them!
Is the object referencing the delegate the same object that instantiated it?
If not, it sounds like you are assuming that the anonymous object's creator
will have a reference to it, I find this highly unlikely.
> This is incorrect. Subscribing to another object's event does not
> constitute a strong reference from the *event sink* to the *event
> source*. The *event source* can disappear at any point. The *event
> sink* cannot disappear without the *event source* first being disposed
> of.
I'm sorry I believe you are incorrect! There may be a term problem, in what
you & I are interpreting what the other said.
event source = object that has an event that is raised
event sink = object that handles an event
Delegate = a class that contains a reference to a method & an object, when
you invoke the delegate the method is executed on that object.
MulticastDelegate = A linked list of Delegates
Anonymous method = an unnamed method created 'on the fly' that is commonly
the target of a delegate.
Anonymous delegate = ??? (possibly identified problem) Technically I am
using Anonymous delegate as synonymous with Anonymous method. In my previous
post every place I used Anonymous delegate use Anonymous method instead.
The source maintains a MulticastDelegate to objects with methods that need
to be executed.
The source always has a reference to the sink, as the source calls into the
sink. If you dereference the sink, the source will still have a reference to
it and it lingers. If you dereference the source, it will go away, as the
sinks do not reference it.
> Delegates do not keep track of which invocation lists they are part of.
What do you consider a Delegate? As I consider the delegate itself to be the
invocation list. Do you consider the target method itself to be the delegate
(not the reference to the method, but the method itself).
Although talking about Who references Whom with Delegates makes my head
spin. I am reasonable confident I am correct. (luckily I do not spew green
pea soup).
Hope this helps
Jay
> > Matt,
> >
[quoted text clipped - 96 lines]
> >>
> >>Matt.