delegate instance and target (C# in depth)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • puzzlecracker

    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)?
  • Peter Duniho

    #2
    Re: delegate instance and target (C# in depth)

    On Tue, 21 Oct 2008 11:01:50 -0700, puzzlecracker <ironsel2000@gm ail.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.
    Gets the class instance on which the current delegate invokes the instance method.


    Pete

    Comment

    • Ignacio Machin ( .NET/ C# MVP )

      #3
      Re: delegate instance and target (C# in depth)

      On Oct 21, 2:01 pm, puzzlecracker <ironsel2...@gm ail.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.

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: delegate instance and target (C# in depth)

        puzzlecracker <ironsel2000@gm ail.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.Save Document;

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

        --
        Jon Skeet - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon.skeet
        C# in Depth: http://csharpindepth.com

        Comment

        Working...