title: Leak in RuntimeBinder when using "dynamic" keyword with __ComObject
body: <p>Does anybody know if there is a way of preventing a memory leak in RuntimeBinder when using "dynamic" keyword with __ComObject instances in C#?</p>
  
  <p>I got the following code:</p>
  
  <pre><code>var t = Type.GetTypeFromCLSID(new Guid("BB06C0E4-D293-4f75-8A90-CB05B6477EEE"));
  while (true)
  {
      dynamic o = System.Activator.CreateInstance(t);
      Marshal.ReleaseComObject(o);
  }
  </code></pre>
  
  <p>This leaks LocalVariableSymbol class instances (and other from the Microsoft.CSharp.RuntimeBinder.Semantics namespace).</p>
  
  <p>Replacing "dynamic" with "object" i.e.:</p>
  
  <pre><code>    object o = System.Activator.CreateInstance(t);
  </code></pre>
  
  <p>fixes the leak but I'd prefer to keep using dynamic (the actual code is much more complex and makes use of "dynamic").</p>
  
  <p>I know the RuntimeBinder singleton caches the data and this causes the leak but do you know if there's any way to cleanup the cache etc.?</p>
  
  <p>Many thanks!</p>
  
  <hr>
  
  <p>Similar questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/33080252/memory-overflow-having-an-increasing-number-of-microsoft-csharp-runtimebinder-s">Memory Overflow: Having an increasing number of Microsoft.CSharp.RuntimeBinder.Semantics</a></li>
  <li><a href="https://stackoverflow.com/questions/8757785/memory-leak-in-clr-classes">Memory leak in CLR classes</a></li>
  </ul>
  
  <p>Related links:</p>
  
  <ul>
  <li><a href="https://connect.microsoft.com/VisualStudio/feedback/details/1925659" rel="nofollow noreferrer">https://connect.microsoft.com/VisualStudio/feedback/details/1925659</a> (retired by Microsoft)</li>
  <li><a href="https://github.com/dotnet/roslyn/issues/2887" rel="nofollow noreferrer">https://github.com/dotnet/roslyn/issues/2887</a></li>
  </ul>