Expired delegate (events)

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

    #1

    Expired delegate (events)

    When an object goes out of scope, do all the event handlers automatically
    get removed, or must this be done manually to ensure that the object
    succeeds in being disposed of?

    Thanks,
    Jon


  • Lee Alexander

    #2
    Re: Expired delegate (events)

    If the object is attached to another one that is not collectable then it
    will also be marked as *not* collectable. So in general yes you should
    remove your handler from the event if you don't want your object to hang
    around.

    Regards
    Lee

    "Jon Davis" <jon@REMOVE.ME. PLEASE.jondavis .net> wrote in message
    news:u14iLJQUDH A.1576@TK2MSFTN GP12.phx.gbl...[color=blue]
    > When an object goes out of scope, do all the event handlers automatically
    > get removed, or must this be done manually to ensure that the object
    > succeeds in being disposed of?
    >
    > Thanks,
    > Jon
    >
    >[/color]


    Comment

    • Simon Trew

      #3
      Re: Expired delegate (events)

      The problem I think is that the publisher of an event is not generally the
      one that can make the decision about whether the subscriber is collectable
      or not-- that's a job more properly left to the subscriber. The current
      event model is thus that the subscriber is somehow informed that the
      publisher (form) would like to go away and it can then unsubscribe to break
      the circular reference.

      It would be quite easy to implement a WeakDelegate/WeakMulticastDe legate
      where the target was a WeakReference, this would give you the behavior you
      wanted. Someone is bound to reply that this is not possible as Delegate and
      MulticastDelega te cannot be derived from (in C#). But you could make your
      own separate classes. If I were to do this, actually I would allow the
      WeakMulticastDe legate to have a combination of normal Delegate and
      WeakDelegate members in its invocation list.

      S.

      "Jon Davis" <jon@REMOVE.ME. PLEASE.jondavis .net> wrote in message
      news:#HDEfoSUDH A.2092@TK2MSFTN GP10.phx.gbl...[color=blue]
      > My hope was that "delegate collections" of events were intrinsically[/color]
      treated[color=blue]
      > differently than standard lists, in this manner, such that if the
      > event-raising object went out of scope it would drop all delegate
      > subscriptions. Apparently not, then, according to your answer, which is a
      > note of severe repercussions on any WinForms app with event-handled
      > controls.
      >
      > Jon
      >
      >
      > "Lee Alexander" <lee@No_Spam_Pl ease_Digita.com > wrote in message
      > news:%23tTO3jQU DHA.3700@tk2msf tngp13.phx.gbl. ..[color=green]
      > > If the object is attached to another one that is not collectable then it
      > > will also be marked as *not* collectable. So in general yes you should
      > > remove your handler from the event if you don't want your object to hang
      > > around.
      > >
      > > Regards
      > > Lee
      > >
      > > "Jon Davis" <jon@REMOVE.ME. PLEASE.jondavis .net> wrote in message
      > > news:u14iLJQUDH A.1576@TK2MSFTN GP12.phx.gbl...[color=darkred]
      > > > When an object goes out of scope, do all the event handlers[/color][/color]
      > automatically[color=green][color=darkred]
      > > > get removed, or must this be done manually to ensure that the object
      > > > succeeds in being disposed of?
      > > >
      > > > Thanks,
      > > > Jon
      > > >
      > > >[/color]
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...