delegates and garbage collection

  • Thread starter Thread starter Daniel Billingsley
  • Start date Start date
D

Daniel Billingsley

I don't know where I got the idea, from reading it somewhere, or from some
speaker, or maybe discussion with a colleague.. but I seem to remember the
statement that heavy use of delegates has a cost in that there's so many
cross-references which can make garbage collection difficult. I seem to
recall the issue being that the references via the delegates don't go away
quickly when an object is discarded.

In particular I am considering the scenario described in this MSDN article
if anyone wants to take the time to read it.
http://www.msdn.microsoft.com/vbasi...library/en-us/dnadvnet/html/vbnet12232003.asp

I guess the two big questions I have are
1) is this really true (in general and in the specific scenario of the
article) [is that two questions?]
2) is there a standard practice or pattern for UNhooking the delegates if
that's necessary

..
 
Hi Daniel,

Based on my understanding, you want to know the correctly usage of delegate
with GC.

Actually, delegate is the same as other reference type, it has nothing
special. Once the class instance the delegate belongs to is out of
scope(Not refered by any more), the entire instance(including delegate)
will be marked for garbage collection.

But sometimes, you may be not aware of this, for example, if instance A's
delegate is referring instance B's method(so it is called event handler),
then you think instance B is out of scope, but it is not "marked for
garbage collection", because instance A is still referring him.

So we should de-wire the event handler for the delegate when does not need
it, for more information, please refer to:
http://weblogs.asp.net/fbouma/archive/2003/07/11/9980.aspx

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
The scenario I'm considering is where ClassA has a delegate wired to a
method of ClassB but it's done by ClassA. I see now that it's not really an
issue for me... in my case either a)ClassA will have to have a reference to
ClassB anyway or b) the ClassB method will be static. In either case I
won't have the "orphan" delegate problem of a ClassB instance going out of
scope but not the ClassA instance.

"if instance A's delegate is referring instance B's method(so it is called
event handler),"
That's not necessarily true, is it, as in my example above? The event
keyword just creates the public property around the delegate for the
external registration, right?
 
Hi Daniel,

Thanks very much for your feedback.

I am glad my reply makes sense to you. For your further question, yes, to
use delegate, there are 2 ways:
1. use event to wrap the delegate, then use +/- operator to add "event
handler" to the event chain.
2. directly use System.Delegate class's methods, such as Delegate.Combine
to asscoate the delegate chain.

The event is just the wrap(Only for easy use) for the delegate, you may
also use the delegate without event(#2).

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Daniel,

Does my reply make sense to you? Do you still have concern on this issue?

Please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top