Question about garbage collector/events

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

    Question about garbage collector/events

    Hey guys
    Does a garbage collector destoy objects that have no references, but
    are subscribed to some other (say singleton) object?

    Thanks in advance

  • Marc Gravell

    #2
    Re: Question about garbage collector/events

    Yes is the short answer. Which means that if the singleton is held as a
    static field (which will never be collected), then the object will
    remain visible (by the singleton) until the event is unhooked. So no
    collection. If you need a collectable pseudo-reference, then look at
    the WeakReference class; in particular, you could perhaps make the
    target implement an interface which the calling code could check for...
    e.g.

    foreach(WeakRef erence wr in refs) {
    if(wr.IsAlive) {
    ISomeInterface obj = wr.Target as ISomeInterface;
    if(obj!=null) {
    obj.SomeMethod( );
    }
    } // plus some automatic removal code (outside of the foreach)...
    }

    Marc

    Comment

    • Marc Gravell

      #3
      Re: Question about garbage collector/events

      AARRGGH; syntax reversal (my bad).... "NO" is the answer. No No No. In
      my head I had your question reversed (does it stay alive). But
      hopefully the detail of my answer made that clear... I'll go get a
      coffee...

      Marc

      Comment

      • =?Utf-8?B?TWFyayBSLiBEYXdzb24=?=

        #4
        RE: Question about garbage collector/events

        When an object exposes an event, under the hood there is really a
        MulticastDelega te instance, which has an invocation list containing
        references to all of the delegates it must call when the event is raised
        (when the event is raised each of the delegates in the list is called one by
        one). So when you subscribe to an event with your object, the delegate that
        gets added to the events invocation list has a reference to your object. So
        as Marc said, if there are no other references to your object, but it
        registered for the singletons event then this object will live at least as
        long as it is subscribed to the event. Marc's suggestion of using
        WeakReferences is a great idea, otherwise you will have to make sure you
        unregister for the event just before you have finished with the object.

        Mark.
        --



        "Harley84" wrote:
        Hey guys
        Does a garbage collector destoy objects that have no references, but
        are subscribed to some other (say singleton) object?
        >
        Thanks in advance
        >
        >

        Comment

        • Harley84

          #5
          Re: Question about garbage collector/events

          Thanks, you've really helped


          Marc Gravell ëúá:
          Yes is the short answer. Which means that if the singleton is held as a
          static field (which will never be collected), then the object will
          remain visible (by the singleton) until the event is unhooked. So no
          collection. If you need a collectable pseudo-reference, then look at
          the WeakReference class; in particular, you could perhaps make the
          target implement an interface which the calling code could check for...
          e.g.
          >
          foreach(WeakRef erence wr in refs) {
          if(wr.IsAlive) {
          ISomeInterface obj = wr.Target as ISomeInterface;
          if(obj!=null) {
          obj.SomeMethod( );
          }
          } // plus some automatic removal code (outside of the foreach)...
          }

          Marc

          Comment

          • Victor Rosenberg

            #6
            Re: Question about garbage collector/events

            Is the idea you're suggesting to rewrite the events mechanism myself,
            using weak references? And is there a way to get the strong reference
            count to an object?


            Marc Gravell wrote:
            Yes is the short answer. Which means that if the singleton is held as a
            static field (which will never be collected), then the object will
            remain visible (by the singleton) until the event is unhooked. So no
            collection. If you need a collectable pseudo-reference, then look at
            the WeakReference class; in particular, you could perhaps make the
            target implement an interface which the calling code could check for...
            e.g.
            >
            foreach(WeakRef erence wr in refs) {
            if(wr.IsAlive) {
            ISomeInterface obj = wr.Target as ISomeInterface;
            if(obj!=null) {
            obj.SomeMethod( );
            }
            } // plus some automatic removal code (outside of the foreach)...
            }
            >
            Marc

            Comment

            • Marc Gravell

              #7
              Re: Question about garbage collector/events

              Is the idea you're suggesting to rewrite the events mechanism myself,
              using weak references?
              Only if you actually need a collectable static/singleton event hook.
              Unfortunately the event/delegate mechanism wouldn't really work here in
              the normal "custom backer ["add"/"remove" event implementation]" sense,
              since you'd only have access to a delegate, which may point vaguely at
              a target (while being collectable independently), so if you need this
              type of functionality I was suggesting some kind of
              "Register/Unregister" / "Subscribe/Unsubscribe" pair of methods
              (static/singleton), such that they accept your object (or the
              interface) and add/remove the item to/from a collection of
              WeakReference wrappers ("refs" in my code sample). Does that make
              sense?
              And is there a way to get the strong reference
              count to an object?:
              None that I know of... managed objects simply aren't reference counted,
              so the only thing that would know is the GC.... right before it pulls
              out its scythe.

              Comment

              Working...