title: File handle leak on dynamically loaded DLL caused by activation context
body: <p>I have a dynamically loaded &amp; unloaded DLL which requires COMCTL32.dll >= v6.0 and MSVCR >= v9.0. To ensure that the correct versions are loaded, I enable manifest file generation in Visual Studio project setting, and add this entry to another manifest file:</p>
  
  <p><code>&lt;dependency&gt;<br>
      &lt;dependentAssembly&gt;<br>
          &lt;assemblyIdentity<br>
              type="win32"<br>
              name="Microsoft.Windows.Common-Controls"<br>
              version="6.0.0.0"<br>
              processorArchitecture="*"<br>
              publicKeyToken="6595b64144ccf1df"<br>
              language="*"<br>
          /&gt;<br>
      &lt;/dependentAssembly&gt;<br>
  &lt;/dependency&gt;</code></p>
  
  <p>In a test program I call <code>LoadLibrary()</code> followed by <code>FreeLibrary()</code> of that DLL, and ProcessExplorer indicates that the following File handles were leaked:</p>
  
  <ul>
  <li>C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_6f74963e</li>
  <li>C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83</li>
  </ul>
  
  <p>From disassembly call stack trace I learnt that on <code>LoadLibrary()</code>, an activation context was automatically created and it opens handles to each of these folders. But it appears that the activation context is not deleted on <code>FreeLibrary()</code>.</p>
  
  <p>If I remove the manifest files and set project settings to disable manifest generation, these leaks are gone. However, that way I will be unable to ensure that the correct MSVCR and COMCTL are used, since this DLL is loaded by processes I have no control over.</p>
  
  <p>Is there a way to remove this leak <em>without removing the manifest files</em>?</p>
  
  <p>Thanks!</p>