OK, I think my first question was badly formulated. So here is the intended one:
Do delegates can prevent objects from being garbage collected in .NET
2.0 Compact Framework ? In other words, do they keep a strong
reference on their target ?
After some testing, it looks like the answer is no. Following is my
test code. If anyone can confirm this experimental result, I would be
grateful.
Thanks!
Sébastien
class Program
{
delegate void Foo(object o);
private static Foo _callback;
static void Main(string[] args)
{
object o = new object();
WeakReference w = new WeakReference(o);
_callback += delegate(object obj) {
Debug.WriteLine(string.Format("In callback: {0}", obj != null)); };
_callback(o);
o = null;
GC.Collect();
Debug.WriteLine(string.Format("After GC1: {0}", w.Target != null));
_callback = null;
GC.Collect();
Debug.WriteLine(string.Format("After callback = null: {0}",
w.Target != null));
GC.Collect();
Debug.WriteLine(string.Format("After GC2: {0}", w.Target != null));
Console.ReadLine();
}
}
--
Sébastien Lorion
Software Architect / Architecte organique
[EMAIL PROTECTED]
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com