ObjectDisposedException and setting dispose object to nothing

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

1) How necessary is it to throw the ObjectDisposedException? I see that most
implementations I've come across in the web do not do this? Will I overkill
my class if I throw the ObjectDisposedException in all of my public methods?

Public Sub DoSomething()
If Me.disposed Then
Throw New ObjectDisposedException()
End if
End Sub

2) If I've already implement the dispose method, is it necessary to set the
object to nothing? Does setting it to nothing help the GC to clean it up
sooner?
Dim mycls as MyClass
' do something with mycls obj
mycls.dipose()
mycls = Nothing

Thanks!
 
Teressa,

1) It should only be thrown if your method will fail because the object has
been disposed. So, a method that does not use any of the resources cleared
up in the dispose would not require this. A method that, for example, tries
to use the database connection that was closed by Dispose, could throw this
exception to indicate the problem. In that scenario however, it might be
more accurate to just pass on the exception generated when trying to use the
connection. I personally have never used ObjectDisposedException, and have
never seen it used either.
2) Setting the object to nothing does help the GC clean up the object
sooner. In fact if you never set it to nothing, the object will never be
collected. GC can only occur on an object when all references to it are
gone.
 
Sean Hederman said:
2) Setting the object to nothing does help the GC clean up the object
sooner. In fact if you never set it to nothing, the object will never be
collected.

This is simply wrong. For local variables, setting the variable to
nothing usually makes no difference at all, as the JIT can tell when it
is last read. For instance variables, you only need to set the variable
to nothing if you expect the containing object to live longer than the
referenced object needs to.
GC can only occur on an object when all references to it are
gone.

Indeed - but that doesn't require anything to be set to nothing.

Setting variables' values to nothing (you can never set an *object* to
nothing) unnecessarily clutters up code, reducing readability.
 
Hi Jon - I'm not sure I follow you discussion to the "set nothing" topic.
I've acutally tried and succeeded in setting an object to nothing, the watch
(in debug mode) showed that the object equals to nothing. So is my last line
of code not neccessary?

Dim mycls as MyClass
' do something with mycls obj
mycls.dipose()
mycls = Nothing
 
Jon's right, sorry for not being clear enough. As he said, the only time you
need to specifically set the reference to nothing is if you want it to be
garbage collected before the containing object. As an example, consider a
service which every hour or so creates an object which it uses and then
discards fairly quickly. It could then make sense to ensure that the object
is available for garbage collection after you're finished with it,
especially if it's a big object. In such a scenario, you would set the
reference to Nothing.

However, most objects lifetimes are tied to their parent, and/or their
scope. For these, it is not neccesary to set the reference to Nothing, since
the child objects will be available for garbage collection once their
references go out of scope, or their parent is collected. As Jon noted,
references may actually be cleared before they go out of scope, if the JIT
figures out that they're not used anymore.

So, to answer your specific question, you would only need to consider
setting mycls to Nothing if the object that contained it would survive
significantly longer than mycls, AND mycls was not going out of scope.
 
Hi Teresa

You may find these blog entries helpful:

Throwing ObjectDisposedException:
http://weblogs.asp.net/clyon/archive/2004/09/23/233464.aspx

General Dispose Info:
http://weblogs.asp.net/clyon/archive/2004/09/21/232445.aspx

Setting objects to Nothing (null):
http://weblogs.asp.net/clyon/archive/2004/12/01/273144.aspx

Hope that helps
-Chris

--------------------
1) How necessary is it to throw the ObjectDisposedException? I see that most
implementations I've come across in the web do not do this? Will I overkill
my class if I throw the ObjectDisposedException in all of my public methods?

Public Sub DoSomething()
If Me.disposed Then
Throw New ObjectDisposedException()
End if
End Sub

2) If I've already implement the dispose method, is it necessary to set the
object to nothing? Does setting it to nothing help the GC to clean it up
sooner?
Dim mycls as MyClass
' do something with mycls obj
mycls.dipose()
mycls = Nothing

Thanks!


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
Back
Top