Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6857795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:04:30+00:00 2026-05-27T02:04:30+00:00

I have a C# COM DLL that calls WMI using the System.Management namespace. The

  • 0

I have a C# COM DLL that calls WMI using the System.Management 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.

If I take the same COM DLL and load it into PowerShell using Reflection.LoadFrom it does not leak memory. I have modified the DLL like so and it no longer leaks (still loading into the service with COM):

public class MyComObject
{
    public void CallCom()
    {
        CallSomeWMIStuff();
    }
}

To this. This no longer leaks!

public class MyComObject
{
    public void CallCom()
    {
        //CallSomeWMIStuff();
    }
}

Here’s an example of some of the WMI 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;
                }
            }
}

EDIT: C++ Snippet:

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 ->GetData();

    strLine = (LPCTSTR)bstrData;
    strMessage += strLine;
}
catch (_com_error& err)
{
        _bstr_t desc = GetErrorMessage(err);
        Log("Excepton %S", (const wchar_t*)desc);
        return 0;
}

pCSharpCom ->Release();

Has anyone seen anything like this? We are seeing a similar issue with C++\CLI that’s loading a different WMI related DLL directly.

Eventually, the WMI service will no longer be responsive and I will have to restart that service as well.

Edit:

This has to do with the apartment state of the COM object. Added a CoInitializeEx rather than a CoInitialize. 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 System.Management classes when the thread was STA, I would no longer leak memory!

Any idea why the first one would be STA?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:04:30+00:00Added an answer on May 27, 2026 at 2:04 am

    The problem had to do with the apartment state of the thread creating the COM object. There was one thread that created the COM object as MTA and another thread that was creating the COM object as STA. The STA thread was created first and then led to issues with the MTA threads. This caused the finalizer to block on GetToSTA.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Setup: I have a COM DLL that calls a method inside a managed C#
I have a COM inproc DLL that we are using in our product. Now
I have a VB6 dll that is trying to create a COM object using
I have an application written in PHP that uses a COM dll written in
I have a C++ dll which implements several COM interfaces, that I'm trying to
I have written a COM dll, and wish to register it using regsvr32 myComdll.dll
We have a pretty mature COM dll, which we test using DUnit. One of
I have a python project that calls a c++ wrapper dll that calls a
How do I make instances of and calls to COM components that have been
Right now we have a dll file that contains all the database calls and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.