Finalize vs. Dispose?

T

Tom

If I have a class object where I need to free resources before it is
de-instantiated, should one use Finalize or Dispose in VB.NET? (This is
for a VB.NET class object) I will need to free up things such as
timers, database connections, etc. Normally, the user of my class will
call a STOP public method I coded to actually stop the processing of
the class, but I figure I need something to make sure things are REALLY
closed out, etc.

I am just a little confused as to which one should be used for a class
object. (And exactly what the difference is - the docs are kinda vague
on this) Thanks...

Tom
 
M

Mythran

Tom said:
If I have a class object where I need to free resources before it is
de-instantiated, should one use Finalize or Dispose in VB.NET? (This is
for a VB.NET class object) I will need to free up things such as
timers, database connections, etc. Normally, the user of my class will
call a STOP public method I coded to actually stop the processing of
the class, but I figure I need something to make sure things are REALLY
closed out, etc.

I am just a little confused as to which one should be used for a class
object. (And exactly what the difference is - the docs are kinda vague
on this) Thanks...

Tom

There are some examples out there about which to call. I believe what is
supposed to be done is, in your Finalize code, you call the Dispose(false)
method. In the Dispose method, you free your resources :)

Mythran
 
M

Mattias Sjögren

If I have a class object where I need to free resources before it is
de-instantiated, should one use Finalize or Dispose in VB.NET?

Depends on if the resources are managed or unmanaged. Basically you
should only use a finalizer to free unmanaged resources that can
safely be freed from a thread other than the one the were allocated on
(since the finalizer runs on a background thread).




Mattias
 

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