Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2005

Tip: Looking for answers? Try searching our database.

ObjectDisposedException and setting dispose object to nothing

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Teresa - 14 Jan 2005 02:37 GMT
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!
Sean Hederman - 14 Jan 2005 05:18 GMT
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.

Signature

Sean Hederman

http://codingsanity.blogspot.com

> 1) How necessary is it to throw the ObjectDisposedException? I see that
> most
[quoted text clipped - 19 lines]
>
> Thanks!
Jon Skeet [C# MVP] - 14 Jan 2005 06:51 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Teresa - 14 Jan 2005 14:29 GMT
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

> > 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
[quoted text clipped - 13 lines]
> Setting variables' values to nothing (you can never set an *object* to
> nothing) unnecessarily clutters up code, reducing readability.
Sean Hederman - 14 Jan 2005 16:19 GMT
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 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
[quoted text clipped - 26 lines]
>> Setting variables' values to nothing (you can never set an *object* to
>> nothing) unnecessarily clutters up code, reducing readability.
"Chris Lyon [MSFT]" - 14 Jan 2005 18:38 GMT
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
[quoted text clipped - 15 lines]
>
>Thanks!

Signature

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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.