2

We moved our web system to Windows authentication. After deploying it to production environment we faced memory leak. We defined it was paged pool memory leak (tag Toke) using poolmon.exe util. During recent modification we only added 2 following methods:

using System.DirectoryServices.AccountManagement;


private bool IsLoginValid(string login, string password)
{
    bool isValid = false;
    using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName))
    {
        isValid = pc.ValidateCredentials(login, password);
    }
    return isValid;
}

private bool isMemberOf(string login, string group)
{
    bool result = false;           
    using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName))
    {
        using (UserPrincipal user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, login))
        {
            if (user != null)
            {
                result = user.IsMemberOf(pc, IdentityType.Name, group);
            }
        }
    }
    return result;
}

Please, help to identify the exact point of leaking and if possible provide with a workaround. Thank you.

Tafari
  • 2,639
  • 4
  • 20
  • 28
mike_grinin
  • 167
  • 1
  • 3
  • 13

2 Answers2

0

There is possibly a bug in the implementation of PrincipalContext and/or UserPrincipal causing failure to auto-dispose of an instance. I have seen this previously. You can easily confirm/fix this by replacing the using by a try-finally, as below.

PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName);
try
{
     isValid = pc.ValidateCredentials(login, password);
}
finally
{
     pc.Dispose();
}
Community
  • 1
  • 1
groverboy
  • 1,133
  • 8
  • 20
  • the `new` keyword alongside explicit interface implementation causing a try/finally call to differ from a using call. Ouch. To check if this is the case, simply add an explicit cast in the finally block and see if the behavior is different: `((IDisposable)pc).Dispose();` – Avner Shahar-Kashtan Oct 30 '13 at 11:51
  • 1
    Thank you but I found that method `UserPrincipal.FindByIdentity()` has memory leak. – mike_grinin Nov 13 '13 at 09:12
0

Method UserPrincipal.FindByIdentity() has memory leak.

mike_grinin
  • 167
  • 1
  • 3
  • 13