Michel,
You never see me writing that the dispose method should not be invoked,
especially by Drawing, Sharepoint it is know that it uses many resources.
(The first because it is using those, the latter because most objects are
simply wrappers about com objects). I did not use the Directory Service
(although I used MPF) and I do this from home in the weekend, when I would
use the DirectoryService I would use it only in an Using clause, simply
because I am suspicious about everything that holds LDAP.
In the System.Data namespace, there is taken effort to make it without
unmananaged resources and if not, encapsulate the disposing in the close.
And what happens, when it is about disposing in this newsgroup, it is always
about system data and not about Drawing, DirectoryService, Sharepoint or
Modal Forms.
Have a look at all the classes which inherit component and therefore
implement IDisposable.
Inheritance Hierarchy
System.ComponentModel..::.Component
Microsoft.ManagementConsole.Advanced..::.WaitDialog
Microsoft.VisualBasic.Compatibility.VB6..::.BaseControlArray
System.CodeDom.Compiler..::.CodeDomProvider
System.ComponentModel..::.BackgroundWorker
System.Configuration.Install..::.Installer
System.Data.Common..::.DataAdapter
System.Data.Common..::.DbCommand
System.Data.Common..::.DbCommandBuilder
System.Data.Common..::.DbConnection
System.Diagnostics..::.EventLog
System.Diagnostics..::.EventLogEntry
System.Diagnostics..::.PerformanceCounter
System.Diagnostics..::.Process
System.Diagnostics..::.ProcessModule
System.Diagnostics..::.ProcessThread
System.DirectoryServices..::.DirectoryEntry
System.DirectoryServices..::.DirectorySearcher
System.Drawing.Printing..::.PrintDocument
System.IO..::.FileSystemWatcher
System.IO.Ports..::.SerialPort
System.Management..::.ManagementBaseObject
System.Management..::.ManagementEventWatcher
System.Management..::.ManagementObjectSearcher
System.Media..::.SoundPlayer
System.Messaging..::.Message
System.Messaging..::.MessageQueue
System.Net.NetworkInformation..::.Ping
System.Net..::.WebClient
System.Runtime.Remoting.Services..::.RemotingClientProxy
System.Runtime.Remoting.Services..::.RemotingService
System.ServiceProcess..::.ServiceBase
System.ServiceProcess..::.ServiceController
System.Timers..::.Timer
System.Web.Services.Protocols..::.WebClientProtocol
System.Web.UI.WebControls..::.Style
System.Windows.Forms..::.BindingSource
System.Windows.Forms..::.ColumnHeader
System.Windows.Forms..::.CommonDialog
System.Windows.Forms..::.Control
System.Windows.Forms..::.DataGridColumnStyle
System.Windows.Forms..::.DataGridTableStyle
System.Windows.Forms.Design..::.FolderNameEditor..::.FolderBrowser
System.Windows.Forms..::.ErrorProvider
System.Windows.Forms..::.HelpProvider
System.Windows.Forms..::.ImageList
System.Windows.Forms..::.Menu
System.Windows.Forms..::.NotifyIcon
System.Windows.Forms..::.StatusBarPanel
System.Windows.Forms..::.Timer
System.Windows.Forms..::.ToolBarButton
System.Windows.Forms..::.ToolStripItem
System.Windows.Forms..::.ToolStripPanelRow
System.Windows.Forms..::.ToolTip
Be aware that this are not all, by instance System.Windows.Forms..::.Control
is the base for a bunch of other classes
Do you really mean ,that you are for all the objects that can be created
with these classes, call the dispose or use the using method when they go
out of scoop or when the program close
They all contain that red sign from you.
Be aware I am very suspicious with all classes which I don't know and derive
from component when I don't see them in a designer.
Cor
Michel Posseth said:
Hello Cor ,
Obviously i am talking about every object that exposes the idisposable
interface
If someone calls a custom method Dispose and even bether is filling the
method with BL code he/she is obviously not knowing what he/she is doing
As said before by me and others the Idisposable interface is a contract ,
and a contract you may not break without consequences
Laws and contracts are there to be followed and to prevent anarchy
ofcourse it could be a coders choice to embrase a state of lawlessness and
wipe there asses with contracts
however those people should not be surprised if there programs at one day
stopped working or consume a lot of resources .
Now i show you some code you might remember from some while ago posted in
this newsgroup by a person having a "memory leak" in his app
A:
Public Function CountADComputers(ByVal strADPath As String) As Integer
Dim intCount As Integer
Dim entry As New DirectoryServices.DirectoryEntry(strADPath)
Dim mySearcher As New
System.DirectoryServices.DirectorySearcher(entry)
mySearcher.PageSize = 1000
mySearcher.Filter = "(objectClass=Computer)"
intCount = mySearcher.FindAll.Count
mySearcher.Dispose()
mySearcher = Nothing
entry.Dispose()
entry = Nothing
Return intCount
End Function
is the above code good ? ....................
Wel actually it did look good to me , however this code ( try it if you
want as it is runable code ) in a iteration will never release memory the
memory usage will grow on every call to the above method
This code that i used in my company that does the same thing did not
have this problem
B:
Public Function GetADComputers(ByVal strADPath As String) As Integer
Dim retval As Integer = 0
Using entry As New DirectoryServices.DirectoryEntry(strADPath)
Using mySearcher As New DirectoryServices.DirectorySearcher(entry)
With mySearcher
.PageSize = 1000
.Filter = "(objectClass=Computer)"
End With
Using returnValue As DirectoryServices.SearchResultCollection =
mySearcher.FindAll
retval = returnValue.Count
End Using
End Using
entry.Close()
End Using
Return retval
End Function
What is the differnce ? well i check every interface and sub interface
for the presence of close and the idisposable interface and wrap it in a
using statement
i guess your code would be something like this
C:
Public Function CountADComputers(ByVal strADPath As String) As Integer
Dim intCount As Integer
Dim entry As New DirectoryServices.DirectoryEntry(strADPath)
Dim mySearcher As New
System.DirectoryServices.DirectorySearcher(entry)
mySearcher.PageSize = 1000
mySearcher.Filter = "(objectClass=Computer)"
intCount = mySearcher.FindAll.Count
Return intCount
End Function
wich in fact would work fine as long as you do not call it to much , and
regularly restart the app
But the only version that could be implemented in a long running app and /
or on a computer with not to much resources is version B i have run
thousands of calls to that method without anny problems
while version A would bomb your app after just a few hundred iterations
version C well i guess we both now that answer but version B can be used
as long as you would like it .
The above case is a perfect example of a discussion we had here multiple
times that when a object that exposes a close and / or dispose method
these
should be called when finished with it .
Telling someone that it is not necesary is like telling that you do not
need to stop at a red trafic light when you do not see other trafic
comming that might cross your path
you might just have missed that one car or motorcyclist that comes with
high speed to your path .
HTH
And regards
Michel Posseth [MCP]
http://www.linkedin.com/in/michelposseth
Cor Ligthert said:
Michel,
The previous is to show that not every dispose should be derived from
component
But the in the message was with dots in fact this one.
There is a warning at the dispose.
\\\
Module Module1
Sub Main()
Dim Michel As System.Data.SqlClient.SqlCommand
Try
Dim a = Michel.ExecuteScalar
Michel = New System.Data.SqlClient.SqlCommand
Catch ex As Exception
'Handle the error
Finally
Michel.Dispose()
End Try
End Sub
End Module
///
The SQL command does not hold any unmanaged resource by the way, so the
only benefit for some can be that some early cleaning will be done at
programming time and not at finalize time by the GC if the dispose method
would be invoked. But that is a complete other discussion.
Before you write it, I advice to use "Using" in this kind of situations,
and I did in that thread.
Cor