HighTechTalks DotNet Forums  

Garbage Collection Question

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss Garbage Collection Question in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Michael Billard
 
Posts: n/a

Default Garbage Collection Question - 07-18-2003 , 05:15 PM






I'm writing a Windows application using C#. Let's say I
create MyObject, and MyObject creates MySubObject,
holding the reference in m_SubObject. MySubObject has an
event, MyEvent, and MyObject implements an event handler
for it. Now let's say I have a form called MyForm, which
consumes MyObject. I finish up with MyObject, setting my
reference to null.

It is my understanding that MyObject still has a
reference to MySubObject in m_SubObject, and MySubObject
has a reference back to MyObject through the event
MyEvent. It seems to me that MyObject and MySubObject
have references to each other (I guess technically the
event's delegate has the reference back to MyObject),
though nobody has a reference to MyObject.

My question is, will the Garbage Collector figure out
that nobody needs MyObject anymore, and get rid of it
along with MySubObject? Or, do I need to manually
disconnect MyObject from MySubObject's event interface?
Thanks for clarifying.

Reply With Quote
  #2  
Old   
Andre
 
Posts: n/a

Default Re: Garbage Collection Question - 07-24-2003 , 03:10 AM






Circular references might be a problem in Reference-Counting GCs (but is
not anymore - see Bacon's paper on this). A Generational Garbage
collector (the CLR GC) does not require reference counts and has a very
simple and elegant way of doing this. Leave everything to the GC.

-Andre

Chris Lyon [MSFT] wrote:
Quote:
Hi Michael

Mattias is right. The GC decides if things are garbage by tracing root references (and so is not confused by circular references). If MyObject has the only reference to
MySubObject, and the reference to MyObject is made null, then MySubObject is unreachable and is garbage.


-Chris

--------------------

From: Mattias Sjögren <mattias.dont.want.spam (AT) mvps (DOT) org
Subject: Re: Garbage Collection Question
Date: Fri, 18 Jul 2003 23:47:00 +0200
References: <03f001c34d71$bebdaa50$a301280a (AT) phx (DOT) gbl
X-Newsreader: Forte Agent 1.8/32.548
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Message-ID: <OpzgVYXTDHA.1688 (AT) TK2MSFTNGP11 (DOT) phx.gbl
Newsgroups: microsoft.public.dotnet.framework.clr
NNTP-Posting-Host: g150.ryd.student.liu.se 130.236.238.150
Lines: 1
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.clr:7371
X-Tomcat-NG: microsoft.public.dotnet.framework.clr

Michael,


My question is, will the Garbage Collector figure out
that nobody needs MyObject anymore, and get rid of it
along with MySubObject? Or, do I need to manually
disconnect MyObject from MySubObject's event interface?

The GC will handle it.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.






Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.