In the MSDN entry for PrincipalSearcher.FindByIdentitiy()
, Gary Caldwell noted (at the bottom in Community Content) that a memory leak results from the use of this method because "the underlying implemenation uses DirectorySearcher
and SearchResultsCollection
but does not call dispose on the SearchResultsCollection
as the document describes." This leak also apparently results in the need to call an explicit Dispose()
when using PrincipalSearcher.FindAll()
or PrincipalSearcher.FindOne()
as a workaround.
This entry was made for .NET 3.5, yet no mention of the problem is listed for .NET 4.0 and later. Can anyone confirm if this issue has been fixed?
A quick look with Reflector suggests that it's been fixed:
System.DirectoryServices.AccountManagement.ADStoreCtx
FindPrincipalByIdentRefHelper
:
DirectorySearcher searcher = new DirectorySearcher(this.ctxBase);
SearchResultCollection results = null;
try
{
...
}
catch (COMException exception)
{
throw ExceptionHelper.GetExceptionFromCOMException(exception);
}
finally
{
searcher.Dispose();
if (results != null)
{
results.Dispose();
}
}