Question about garbage collector/events

H

Harley84

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

Thanks in advance
 
M

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(WeakReference 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
 
M

Marc Gravell

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
 
G

Guest

When an object exposes an event, under the hood there is really a
MulticastDelegate 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.
 
V

Victor Rosenberg

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?
 
M

Marc Gravell

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top