Destroying objects

S

steve

Hi All

I need some advice on the correct way to dispose of an object after use

I am using VB2005

e.g
-------------------------------------------------------------------------------------------------------
dim dt as new datatable, sql as string

try
sql = "select * from ...."
dt = getdata(sql) ' my sub for retrieving data

do some stuff.....

catch ex as oledbexception
msgbox(ex.message)

finally
if not dt is nothing then
dt.dispose
dt = nothing
end if
end try
-------------------------------------------------------------------------------------------------------------
1. Do I need to set dt to nothing after calling the dispose method??

2. If an object does not have a dispose method, is setting it to nothing
sufficient to release resources?

3. VB 2005 no longer requires declaring a form variable before showing it.
Do you still have to destroy it with frm1.dispose and frm1 = nothing?


Regards
steve
 
C

Cor Ligthert [MVP]

Steve,

-----
1. Do I need to set dt to nothing after calling the dispose method??

No because it goes in this situation out of scope and than the stack will be
removed and everything declared in the methode (with exception from static
declarations) is set to nothing. (That does not mean that it always will be
released. As there is a reference *to* it, the GC (Garbage Collector) will
investigate that first and than not release it.
2. If an object does not have a dispose method, is setting it to nothing
sufficient to release resources?

No because the meaning of managed code is that the GC does that for you, you
don't have to use dispose or setting to nothing in most cases. Only there
where you are sure that unmanaged resources (that is not unmanaged code).
you have to dispose.

Have a look to the text on this page after the long table in this article.

http://msdn.microsoft.com/library/d...ref/html/frlrfsystemidisposableclasstopic.asp
3. VB 2005 no longer requires declaring a form variable before showing it.
Do you still have to destroy it with frm1.dispose and frm1 = nothing?
No you cannot even prevent that if you or the user closes that.

If it is opened with showdialog (as a modal dialog) than you have to use
dispose to release the unmanedresources. (This is one of the exceptions
where it is needed).

I hope this helps,

Cor
 
J

Jeffrey Tan[MSFT]

Hi steve,

Thanks for your post.
Dispose method is used to release the unmanaged resource used by the class
object. If the code snippet above is embeded in the same method, then there
is no need to set dt to nothing. This is because the dt variable is created
on the stack(it is the local variable, not class level variable), after
exiting the method, it will automatically being destroyed. So at some later
time, GC will reclaim its managed resource(Because there is no reference
referring this managed class object).
sufficient to release resources?
Yes, without a Dispose method means it does not use the unmanaged
resource(Or it does not expose the method to reclaim unmanaged resource).
So all we should do is reclaiming its managed resource. Then setting the
reference to nothing is the only work need to do.(also, again, if your
reference variable is on the stack, there is no need to set it to nothing,
because stack will destroy the reference after method calling)
it. Do you still have to destroy it with frm1.dispose and frm1 = nothing?
No, there is really the Form reference, but it is hidden by the VB2005. If
you click Project-> "Show all files" option, you will find
"Application.Designer.vb" file in "My Projects" directory in the Solution
Explorer. In this file, you can find the reference like this:
Global.VB2005Test.Form1

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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