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 / Languages / Managed C++ / June 2004

Tip: Looking for answers? Try searching our database.

Delete and Garbage Collection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peteroid - 04 Jun 2004 19:18 GMT
A few questions regarding delete and garbage collection (gc).

First, is something not ordinarily gc which is created via new in a garbage
collected class also garbage collected? That is:

__gc myGCClassA
{
   public:
   myGCClassA()
   {
           m_String = new char[100] ;
   }

   ~myGCClassA()
   {
           delete [] m_String ;  // ***is this necessary to free memory? if
not, is it ok? ***
   }

   private:
   char* m_String ;
} ;

Second, is it ok to delete something 'pre-maturely' in a gc class? That is,

__gc myGCClassB
{
   public:
    myGCClassB()
   {
       m_A = new myGCClassA() ;
   }

   ~myGCClassB() {} ;

   void FreeA()
   {
       delete m_A ;    // *** is this ok to do? ***
   }

   private:
   myGCClassA*        m_A ;
} ;

Next, is it true that a GC instance created in a non-gc class WILL gc itself
anyway?

Finally, when a gc class instance is destroyed for any reason or deleted,
does it still execute the destructor?

Thanx! : )

 [==Peter==]
mccoyn - 04 Jun 2004 21:01 GMT
My understanding is that anything which is __nogc will not be automatically cleaned up by the gc

"First, is something not ordinarily gc which is created via new in a garbag
collected class also garbage collected?

No, you have to clean it up yourself exactly as you suggested.  Adding a destructor with a cleanup statement

"Second, is it ok to delete something 'pre-maturely' in a gc class?

Yes, as long as you don't try to use it afterwards, or delete it again

"Next, is it true that a GC instance created in a non-gc class WILL gc itsel
anyway?

No, as long as there is a pointer to the GC instance it won't be collected

"Finally, when a gc class instance is destroyed for any reason or deleted
does it still execute the destructor?

Yes, so its safe to cleanup __nogc references here
David Olsen - 05 Jun 2004 15:53 GMT
> "First, is something not ordinarily gc which is created via new in a
> garbage collected class also garbage collected?"
>
> No, you have to clean it up yourself exactly as you suggested.
> Adding a destructor with a cleanup statement.

But a destructor of a __gc class is really a finalizer.  The finalizer
might get called at some arbitrary time in the future, or it might never
be run at all.

So if you really care about the non-managed resources in a managed
class, then you need to have a cleanup function which releases those
resources, and that function must be called at the correct time in your
code.  A common idiom for this is to implement the IDisposable interface.

Unmanaged memory may or may not be important enough to warrant a cleanup
function.  (That is, a small amount of memory that is held too long
might not cause problems in your program.)  But for some resources, such
as files, network connections, or mutexes, releasing the resource at the
correct time is vital and can't be delayed until the finalizer.

Signature

David Olsen
qg4h9ykc5m@yahoo.com

Jesse McGrew - 05 Jun 2004 09:13 GMT
> Next, is it true that a GC instance created in a non-gc class WILL gc itself
> anyway?

Well, you can't directly store a __gc pointer as a field of a __nogc
class. If you do any funky casting to get around that rule, the garbage
collector won't know about it, so that's dangerous.

You can, however, store a reference to a GC object with the gcroot<>
template, which will keep the garbage collector informed about the
reference. The object won't be garbage collected as long as there's a
gcroot<> holding a reference to it.

Jesse

Rate this thread:







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.