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
