Note that there are some explanatory texts on larger screens.

plurals
  1. POManagementObject leaking in C# COM DLL
    primarykey
    data
    text
    <p>I have a C# COM DLL that calls WMI using the <code>System.Management</code> namespace. The DLL is being loaded into a C++ service. Every time I call the into the WMI classes I'm seeing a HUGE memory leak. After about an hour I am well over 1 GB of memory used. </p> <p>If I take the same COM DLL and load it into PowerShell using <code>Reflection.LoadFrom</code> it does not leak memory. I have modified the DLL like so and it no longer leaks (still loading into the service with COM):</p> <pre><code>public class MyComObject { public void CallCom() { CallSomeWMIStuff(); } } </code></pre> <p>To this. This no longer leaks!</p> <pre><code>public class MyComObject { public void CallCom() { //CallSomeWMIStuff(); } } </code></pre> <p>Here's an example of some of the WMI code:</p> <pre><code>var scope = new ManagementScope( "root\\cimv2" ); scope.Connect(); using (var myservice = GetService("SomeService", scope)) { //Some Stuff } ... ManagementObject GetService(string serviceName, MangementScope scope) { ManagementPath wmiPath = new ManagementPath( serviceName ); using (ManagementClass serviceClass = new ManagementClass( scope, wmiPath, null )) { using (ManagementObjectCollection services = serviceClass.GetInstances()) { ManagementObject serviceObject = null; // If this service class does not have an instance, create one. if (services.Count == 0) { serviceObject = serviceClass.CreateInstance(); } else { foreach (ManagementObject service in services) { serviceObject = service; break; } } return serviceObject; } } } </code></pre> <p>EDIT: C++ Snippet:</p> <pre><code>NAMESPACE::ICSharpComPtr pCSharpCom = NULL; HRESULT hr = pCSharpCom .CreateInstance(NAMESPACE::CLSID_CSharpCom); if (FAILED(hr)) { Log("Failed (hr=%08x)", hr); return hr; } try { _bstr_t bstrData = pCSharpCom -&gt;GetData(); strLine = (LPCTSTR)bstrData; strMessage += strLine; } catch (_com_error&amp; err) { _bstr_t desc = GetErrorMessage(err); Log("Excepton %S", (const wchar_t*)desc); return 0; } pCSharpCom -&gt;Release(); </code></pre> <p>Has anyone seen anything like this? We are seeing a similar issue with C++\CLI that's loading a different WMI related DLL directly. </p> <p>Eventually, the WMI service will no longer be responsive and I will have to restart that service as well. </p> <p>Edit:</p> <p>This has to do with the apartment state of the COM object. Added a <code>CoInitializeEx</code> rather than a <code>CoInitialize</code>. I set the thread to MTA. At first it didn't look like this was working until I realized that first time the method was called we were seeing the thread state set to STA rather than MTA! Every subsequent call would be MTA. If I returned right away, before calling the <code>System.Management</code> classes when the thread was STA, I would no longer leak memory! </p> <p>Any idea why the first one would be STA?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload