destrucor & GC ?

S

Sagaert Johan

Hi

If my destructor of my class (derived from usercontrol ) never gets called ,
does this mean that its not collected by the GC and hence
allocated memory is not freed ?

Is there a way to find out whats prevents the GC to collect this object ?

Johan
 
N

Nicholas Paldino [.NET/C# MVP]

Sagaert,

It's not a destructor, it is a finalizer.

If it never gets called, that doesn't mean that it is never garbage
collected. If you have passed your object reference to the static
SuppressFinalize method on the GC class, then your class will never have the
finalizer called. This is typical when you have implemented the IDispose
interface properly (because your implementation will pass itself to the
SuppressFinalize method so that the finalizer is not called, because you
should have done all the cleanup necessary in the Dispose method) and the
Dispose method is called by people using your class.

The only thing that would prevent GC from collecting an object is
because something is holding a reference to that object, and something is
holding a reference to that object, or it is rooted, and so on, and so on.

You should not depend on the finalizer to indicate when an object has
been GC'ed if you have implemented the IDisposable interface.
 

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

Similar Threads


Top