We have 2 objects that have circular references.
I know that the garbage collector is supposed to detect circular references,
but I'm not sure it also does that in our situation.
Let me explain our setup:
We have a custom control (A) that creates an DragAndDrop (B) object, and
passes itself with it.
This DragAndDrop object keeps a reference to the control it is bound to. (B
references A)
It also adds delegates to its own methods to the DragDrop, DragEnter and
DragOver eventhandlers of the control (A references B)
<<example code>>
public class SomeControl : Control
{
public SomeControl() : base()
{
DragAndDrop temp = new DragAndDrop(this);
}
}
public class DragAndDrop
{
private Control control;
public DragAndDrop(Control control)
{
this.control = control;
this.control.DragDrop += new
System.Windows.Forms.DragEventHandler(this.DragDrop);
this.control.DragEnter += new
System.Windows.Forms.DragEventHandler(this.DragEnter);
this.control.DragOver += new
System.Windows.Forms.DragEventHandler(this.DragOver);
}
private void DragOver(object sender, System.Windows.Forms.DragEventArgs
e)
{
//does something usefull
}
private void DragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
//does something usefull
}
private void DragDrop(object sender, System.Windows.Forms.DragEventArgs
e)
{
//does something usefull
}
}
I have 2 questions:
1) Will the garbage collector cleanup the SomeControl and DragAndDrop
instances when the form containing the control is closed and destroyed?
2) If it does, is it OK then that the control doesn't keep a reference to
the created DragAndDrop object? Wouldn't this cause the DragAndDrop object
to be garbage collected immidiately?
Thanks in advance,
Marc Selis
Soft Cell nv
Mattias Sjögren - 12 Jul 2005 12:45 GMT
>1) Will the garbage collector cleanup the SomeControl and DragAndDrop
>instances when the form containing the control is closed and destroyed?
It will clean them up some time after you no longer have any reachable
references to them, which is when you release all references to the
containing form.
>2) If it does, is it OK then that the control doesn't keep a reference to
>the created DragAndDrop object?
Yes
>Wouldn't this cause the DragAndDrop object
>to be garbage collected immidiately?
No
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
"Jeffrey Tan[MSFT]" - 19 Jul 2005 03:34 GMT
Hi Marc,
Does Mattias' reply make sense to you? Is your problem resolved? Please
feel free to tell me, thanks
Best regards,
Jeffrey Tan
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.