473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

delegate instance and target (C# in depth)

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)?
Oct 21 '08 #1
3 3659
On Tue, 21 Oct 2008 11:01:50 -0700, puzzlecracker <ir*********@gmail.com>
wrote:
[...]
1) what does target refer to (delegate instance will prevent its
target from being garbage)?
The target of a delegate is the instance of the object that will be used
when invoking the delegate. Obviously, this applies only for delegates
that refer to instance methods. Static methods require no instance, and
so the target is null in that case.

More literally, the target is the reference returned by the
Delegate.Target property.
http://msdn.microsoft.com/en-us/libr...te.target.aspx

Pete
Oct 21 '08 #2
On Oct 21, 2:01 pm, puzzlecracker <ironsel2...@gmail.comwrote:
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.
Oct 21 '08 #3
puzzlecracker <ir*********@gmail.comwrote:
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 - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 22 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Pieter Breed | last post by:
Hi All, I am trying to use the attribute/reflection system to as much potential as I can think of, but I've run into a snag. I would appreciate it if someone would point me in the right...
5
by: - | last post by:
Hi to All, Is there any trick for adding additional runtime info (for example a string) to a delegate instance? I would like to mimic the Java EventListener - EventListenerProxy,...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
2
by: Peter Rilling | last post by:
This may sound like a weird desire, but is there any way to get a reference to the actual MulticastDelegate that was used to invoke a handler, from within the handler? Maybe through the call stack...
1
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
4
by: niceguybrijesh | last post by:
public delegate bool MyDelegate(); public void MyMethod() { return false; } What is difference between follwoing two statements? 1) MyDelegate aDelegate = new MyDelegate(MyMethod);
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
2
by: Tony Johansson | last post by:
Hello!! Below is a program copied from a book. The program is easy but there is one thing about the program that I would like to have some more info about. Assume that you have for example 10...
7
by: colin | last post by:
Hi, How can I use a delegate that I can set to call a non static function but of any instance of the class ? eg class TypeTeader<T> { delegate T readDelegate<T>();
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.