PC Review


Reply
Thread Tools Rate Thread

delegate instance and target (C# in depth)

 
 
puzzlecracker
Guest
Posts: n/a
 
      21st Oct 2008
A cut-and-paste from the Jon's book, with a question to follow:

Utter garbage! (Or not, as the case may be…)—It’s worth being aware
that a
delegate instance will prevent its target from being garbage
collected, if
the delegate instance itself can’t be collected. This can result in
apparent
memory leaks, particularly when a “short-lived” object subscribes to
an event in a “long-lived” object, using itself as the target. The
long-lived
object indirectly holds a reference to the short-lived one, prolonging
its lifetime

1) what does target refer to (delegate instance will prevent its
target from being garbage)?
 
Reply With Quote
 
 
 
 
Ignacio Machin ( .NET/ C# MVP )
Guest
Posts: n/a
 
      21st Oct 2008
On Oct 21, 2:01 pm, puzzlecracker <ironsel2...@gmail.com> wrote:
> A cut-and-paste from the Jon's book, with a question to follow:
>
> Utter garbage! (Or not, as the case may be…)—It’s worth being aware
> that a
> delegate instance will prevent its target from being garbage
> collected, if
> the delegate instance itself can’t be collected. This can result in
> apparent
> memory leaks, particularly when a “short-lived” object subscribes to
> an event in a “long-lived” object, using itself as the target. The
> long-lived
> object indirectly holds a reference to the short-lived one, prolonging
> its lifetime
>
> 1) what does target refer to (delegate instance will prevent its
> target from being garbage)?


The delegate is like a definition , but the concrete method being
called belong to an instance of a given type. The target is that
instance.

Now a good question is what happen if the target is a static method.
In this case the above should not apply.
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      22nd Oct 2008
puzzlecracker <(E-Mail Removed)> wrote:
> A cut-and-paste from the Jon's book, with a question to follow:
>
> Utter garbage! (Or not, as the case may be=3F)=3FIt=3Fs worth being aware
> that a
> delegate instance will prevent its target from being garbage
> collected, if
> the delegate instance itself can=3Ft be collected. This can result in
> apparent
> memory leaks, particularly when a =3Fshort-lived=3F object subscribes to
> an event in a =3Flong-lived=3F object, using itself as the target. The
> long-lived
> object indirectly holds a reference to the short-lived one, prolonging
> its lifetime
>
> 1) what does target refer to (delegate instance will prevent its
> target from being garbage)?


The instance (or rather, the reference to the instance) that the action
will be called on. It's defined in the previous paragraph

So for example, if I do:

EventHandler x = someObject.SaveDocument;

then the value of "someObject" is the target. When the delegate is
invoked, it's like calling someObject.SaveDocument().

--
Jon Skeet - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
delegate instance colin Microsoft C# .NET 7 24th Apr 2008 05:55 PM
delegate instance creation niceguybrijesh@yahoo.com Microsoft C# .NET 4 2nd Aug 2006 12:19 PM
Emulating Delegate.Target and Delegate.Method rodrigobamboo@gmail.com Microsoft Dot NET Compact Framework 1 14th Nov 2005 01:19 PM
Does delegate instantiate an instance of Delegate or MultiCastDele =?Utf-8?B?TmltYQ==?= Microsoft C# .NET 2 30th Sep 2005 08:00 PM
The delegate must have only one target?? =?Utf-8?B?RWQgQQ==?= Microsoft C# .NET 2 19th Jul 2005 02:51 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:05 AM.