在PrincipalSearcher.FindByIdentitiy()的MSDN entry中,Gary Caldwell指出(在社区内容的底部)使用此方法会导致内存泄漏,因为“底层实现使用了DirectorySearcher和SearchResultsCollection,但并不像文档描述的那样在SearchResultsCollection上调用dispose。”此泄漏显然还导致在使用PrincipalSearcher.FindAll()或PrincipalSearcher.FindOne()作为变通方法时需要调用显式Dispose()。
此条目是为.NET 3.5创建的,但在.NET 4.0及更高版本中未列出该问题。有没有人能确认这个问题是否已经解决?
发布于 2012-11-28 05:16:14
用Reflector快速查看一下,就会发现它已经修复了: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();
}
}https://stackoverflow.com/questions/13592689
复制相似问题