Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / July 2005

Tip: Looking for answers? Try searching our database.

Will this cause a memory leak?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marc Selis - 12 Jul 2005 09:52 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.