473,387 Members | 1,882 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,387 software developers and data experts.

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

Dec 19 '06 #1
6 1529
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

Dec 19 '06 #2
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

Dec 19 '06 #3
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.
--
http://www.markdawson.org
"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

Dec 19 '06 #4
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(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
Dec 19 '06 #5
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(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
Dec 19 '06 #6
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.

Dec 20 '06 #7

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

Similar topics

10
by: pachanga | last post by:
The Hans-Boehm garbage collector can be successfully used with C and C++, but not yet a standard for C++.. Is there talks about Garbage Collector to become in the C++ standard?
7
by: Casey Leamon | last post by:
I've been noticing that all of my .NET apps seem to progressivly use more and more memory. Even after several reworks of the code I can only manage to slow the growth. Is there some Garbage...
4
by: pachanga | last post by:
After you destroy a Object and its send to the garbage collections, can you retrieve the object back? Also, if you can, can you destroy an object permantly with no trace of it?
9
by: Olivier Fermy | last post by:
I have created a sample project where i have referenced an object only with an event : textBox.VisibleChanged += new EventHandler(this.textBox_VisibleChanged); When i call GC.Collect(), the...
2
by: hakan_cn | last post by:
Hi all! Sorry for the cross-post, I really have a difficult problem and need some help badly. I have a VB6 ActiveX component (dll) that listens to a real-time data feed of stock quotes (built...
8
by: Adam Honek | last post by:
Hi, Is there a way to delcare a garbage collector in VB.NET 2005? Thanks, Adam
28
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will...
142
by: jacob navia | last post by:
Abstract -------- Garbage collection is a method of managing memory by using a "collector" library. Periodically, or triggered by an allocation request, the collector looks for unused memory...
1
by: Crash | last post by:
..Net - All versions I have Essential .Net Vol 1 (Don Box) and the Jeffrey Richter articles on the .NET heap - they are both excellent reads. But I am still unclear on the big pinned memory...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.