Will the GC clean this up?

  • Thread starter Thread starter Craig
  • Start date Start date
C

Craig

Newbie C# developer question:

class myStuff
{
private System.Object o;

public void doWork()
{
o = new Sytem.Object;
}
}

Will the GC take care of cleaning up the resource myStuff.o is using when
the myStuff object goes out of scope?

Craig
 
Maybe not when it goes out-of-scope, but the GC will mark it for deletion
and will delete it when the GC feels it's the best time to do so.
 
Yes, so long as the resources that myStuff.o is holding internally are
_managed_. myStuff.o itself will certainly be cleaned up when myStuff
goes out of scope.

The only things that wouldn't be cleaned up would be any pointers to
unmanaged resources, usually gained via calls to non-.NET dlls or the
Windows API, that myStuff.o may be holding. If you have those sorts of
resources, you must be sure to release them via a try...finally, a
using, or a Dispose() method.

As well, there seem to be some managed types that benefit from explicit
disposal. The ones I've run across are Forms and Images, but there may
be other. This seems to speed up the GC process.
 
Uchiha Jax said:
DataTables also really like to be disposed.

I've heard this before, but with no actual evidence. Could you give an
example? I don't think calling Dispose on a DataTable does anything
interesting unless it's in a container.
 
I don't see why you'd say a DataTable wants to be disposed, especially
since DataTables always suppress finalization (it's in the default
constructor) which means that Dispose will not be called unless you
call it yourself (implying it doesn't need to be called at all).

DataTable also doesn't implement Dispose itself at all--the only
implementation it has is what it receives from
MarshalByValueComponent, which removes it from the container if one
exists and broadcasts the disposed event. Besides this, as far as I
can tell a DataTable never beomes sited unless you specifically set
the Site property.

Sam


DataTables also really like to be disposed.

B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
 
Back
Top