Hi Simon,


Please note that the context of your example is not quite the same as the
one that I originally responded to. Calling the Dispose method and thus
invoking an object's clean up is not the same as making an object
unreachable.



Many developers believe that setting an "object = nothing" is the same as
calling its Dispose method. In fact, the example code posted to which I
responded demonstrated that objects are "disposed of in this location" by
setting them equal to nothing! A proper example would have called their
Dispose method instead.



Note too that developers often believe that they need to implement the
IDisposable code pattern whenever any unmanaged object is used by a class.
This is only true if the unmanaged object is defined with class member
scope. Local unmanaged resources should have their Dispose method called in
the "Finally" block of a properly formatted try/catch/finally.



Many developers are also under the mistaken impression that setting an
object to Nothing will destroy it or somehow promote a faster clean up by
the garbage collector -- or feel that it is necessary or somehow desirable
because that the way it was done in VB6. Often former VB6 developers place
extraneous "set object = nothing" statements near the end of every single
routine as if they were laying non-effective sacrifices to the ancient VB6
god. All this effort buys them nothing because it does not make the object's
clean-up code to execute in a deterministic fashion and it seldom has any
impact on the performance of garbage collection.



In placing these "set Nothing" statements at the end of their routines
developers only succeed in delaying the garbage collector from performing
its natural function because the object now becomes reachable until the "set
object = nothing" statement has been reached and is executed. To truly
understand and appreciate this fully one must know that an object can be
garbage collected even before completion of the routine that the object was
created in has finished executing. (BTW: Debug compilations extend the life
of objects for debugging purposes -- so it is hard to witness the phenomenon
that I am describing with the debugger attached; however this is the
behavior or "Release" compiled assemblies with no debugger attached).



--Paul Mehner







-----Original Message-----
From: Unmoderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Hewitt, Simon C.
(Contractor)
Sent: Tuesday, January 25, 2005 10:29 PM
To: [email protected]
Subject: Re: [ADVANCED-DOTNET] Disposing of objects



Whilst I agree with most that has been said, I'm not sure the bit about

the "setting an object to Nothing does nothing useful for you" is

correct in all circumstances - consider the following:-

  If an object is disposed, it doesn't necessary follow that there are

no references to it (e.g. If it was in a collection),

therefore any references the disposed object holds exclusively are still

reachable. By setting these internal references to nothing/null on

disposal (but only when Disposing=true), those owned objects are now

candidates for garbage collection.



Also, I'm sure I've seen in the framework examples of where an object is

disposed, it automatically removes itself from its parent's collection.

(Or the other way where a collection is disposed and it sets all of its

members' Parent property to null and disposed them - I think this was

Control.ControlCollection). Isn't this a fair example of using the

dispose pattern for something that doesn't involve unmanaged resources?



Cheers

Simonx







-----Original Message-----

From: Unmoderated discussion of advanced .NET topics.

[mailto:[EMAIL PROTECTED] On Behalf Of Paul Mehner

Sent: 25 January 2005 18:28

To: [email protected]

Subject: Re: [ADVANCED-DOTNET] Disposing of objects





This IDisposable code pattern should not be placed in all classes. There

is only one "best practice". Placing this pattern on all classes would

be an "ignorant practice". Not is the code pattern not needed for

managed classes, it is not desired either because you are forcing the

Garbage Collector to do things it doesn't need to do and making it less

efficient at performing its work than it would be if you just left it

alone.



The code pattern provides a means for allowing classes that need to

deterministically perform clean up to accomplish that objective. The

Garbage Collector knows what to do (exceedingly well) for all managed

resources.



Read the Garbage Collection chapter in Jeffrey Ricther's book on

Programming the .NET Framework to gain true IDisposable enlightenment.



BTW: Setting an object to Nothing does nothing useful for you. In your

code sample below you have <object_name> = Nothing. Try:

<object_name>.Dispose (or .Close) instead. If an object doesn't have a

.Close or .Dispose method you are almost certainly barking up the wrong

tree trying to dispose of it with the IDisposable code pattern.



--Paul Mehner



-----Original Message-----

From: Unmoderated discussion of advanced .NET topics.

[mailto:[EMAIL PROTECTED] On Behalf Of Knowlton,

Gerald F.

Sent: Tuesday, January 25, 2005 9:37 AM

To: [email protected]

Subject: [ADVANCED-DOTNET] Disposing of objects



Greetings:



We are having somewhat of a lengthy discussion going on here regarding

the disposing of objects.



One camp wants to put the following code into all classes:



Private Disposed as Boolean

.

.

.

Public sub Dispose() Implements IDisposable.Dispose

        Dispose(True)

        GC.Suppressfinalize(me)

End sub



Protected Sub Dispose(ByVal Disposing as Boolean)

        If Disposed then Exit Sub

        If Disposing then DisposeOfObjects

End Sub



Private Sub Dispose of Objects

        <object_name> = nothing

End Sub





While the other camp says it is not needed unless the class has

unmanaged resources.



Our question is; which would be the better practice?



Best regards,





Jerry



==================================================================

All BWXT, Inc.  Email communications are subject to auditing for

adherence to company policy pertaining to waste, fraud and abuse. Misuse

of the e-mail system is cause for disciplinary action.

==================================================================



===================================

This list is hosted by DevelopMentorR  http://www.develop.com



View archives and manage your subscription(s) at

http://discuss.develop.com



===================================

This list is hosted by DevelopMentor(r)  http://www.develop.com



View archives and manage your subscription(s) at

http://discuss.develop.com



===================================

This list is hosted by DevelopMentor.  http://www.develop.com



View archives and manage your subscription(s) at http://discuss.develop.com


===================================
This list is hosted by DevelopMentor�  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to