Delete and Garbage Collection

P

Peteroid

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==]
 
G

Guest

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.
 
J

Jesse McGrew

Peteroid said:
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
 
D

David Olsen

mccoyn said:
"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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top