title: COM object that has been separated from its underlying RCW can not be used - why does it happen?
body: <p>I sometimes get the following exception: 
  COM object that has been separated from its underlying RCW can not be used </p>
  
  <p>Sample code: </p>
  
  <pre><code>using (AdOrganizationalUnit organizationalUnit = new AdOrganizationalUnit(ADHelper.GetDirectoryEntry(ouAdDn))) 
  { 
  using (AdUser user = organizationalUnit.AddUser(commonName)) 
  { 
  //set some properties 
  user.Properties[key].Add(value); 
  
  user.CommitChanges(); 
  
  user.SetPassword(password); //it is set using Invoke 
  
  //must be set after creating user 
  user.Properties["UserAccountControl"].Value = 512; 
  
  user.CommitChanges(); 
  
  } 
  } 
  </code></pre>
  
  <p>AdUser looks like this: </p>
  
  <pre><code>public class AdUser : DirectoryEntry 
  { 
  public AdUser(DirectoryEntry entry) 
  : base(entry.NativeObject) 
  { 
  } 
  
  public bool SetPassword(string password) 
  { 
  object result = this.Invoke("SetPassword", new object[] { password }); 
  return true; 
  } 
  } 
  </code></pre>
  
  <p>This is simplified version of my code. The exception sometimes shows up, sometimes not. Most of the time it happens when I'm trying to set UserAccountControl value. 
  Does anyone know what could be the reason? </p>
  
  <p>I found out that this error happens when I dispose DirectoryEntry the AdUser was created with and I'm still trying to use AdUser object. However this is not the case in the code posted above. Is it possible that DirectoryEntry somehow disposes itself? </p>
  
  <p>I also get this exception when I try to execute operation on many active directory objects. For example when I try to set SecurityDescriptor for one thousand users, I get this error every 200-300 users. When I retry operation after establishing new connections I don't get exception. The message is raceonrcwcleanup was detected. My app is not multithreaded.</p>
  
  <p>Any help would be appreciated.</p>