4

I am using COM to initialize a C# .NET class through c++ unmanaged code, and I am detecting a memory leak even in a very basic program:

int _tmain(int argc, _TCHAR* argv[])
{
 CoInitialize(NULL);
 ComClass::IClass1 *_comClass1;
 HRESULT hr = CoCreateInstance(__uuidof(ComClass::Class1),
         0,
         CLSCTX_INPROC_SERVER,
         __uuidof(ComClass::IClass1),
         reinterpret_cast<void**>(&_comClass1));
 _comClass1->Release();
 CoUninitialize();
 return 0;
}

the C# class is also super simple:

 [ComVisible(true), Guid("A95C4F43-65B0-4706-94D1-BEE2EF416766")]
    public interface IClass1
    {
    }

    [ComVisible(true), Guid("4670C9CD-0501-4274-BF03-E1FF65A77FEC")]
    public class Class1 : IClass1
    {
        public Class1()
        { }
    }

And still I'm detecting memory leaks. I'm using GlowCode and Purify for detecting the leaks, but even without them I can see the memory usage rising.

Am I not using CoCreateInterface correctly? what am I missing?


clarification

This is just a small program I created to simulate the problem. In my real program there are a lot of CoCreateInstance calls, and the VM size rises to around 1.5GB, surely this can't be normal... In addition, I can see using perfmon that the process' private bytes are rising, while the .Net CLR Memory bytes in all Heaps doesn't. Also GlowCode can monitor the mananged heap and does not point to a memory leak in the managed part...

4
  • 1
    When are you detecting these memory leaks? Can you find out the allocations at the beginning and end of main? Perhaps these objects are outside the scope of main (e.g, CRT/ATL allocations that are de-allocated after the fact).
    – Moo-Juice
    Jan 19, 2011 at 15:10
  • @Moo-Juice: Purify identifies the leak at the CoCreateInstance call. Also, If I loop on the CoCreateInstance and Release I can see the memory usage rising.
    – Oded
    Jan 19, 2011 at 15:23
  • At the risk of sounding stupid : Have you tried CLSCTX_INPROC_HANDLER
    – engf-010
    Jan 19, 2011 at 15:32
  • Smells like a red herring to me. I wonder if Purify gets confused because if the non-deterministic nature of C#'s GC. Jan 19, 2011 at 15:52

1 Answer 1

6

Yes, this is normal. The first call to CoCreateInstance will load the CLR into your process. Which creates the primary AppDomain to load the managed code, with a garbage collected heap, the loader heap and a smattering of internal data structures. Calling the Release() method on the interface pointer does not unload the CLR. It sticks around to service any future requests to load and execute managed code. The primary appdomain doesn't get unloaded until the process terminates.

An unmanaged memory diagnostic tool is not going to know that this is normal.

A true memory leak is one that increases the VM size of the process without an upper bound. Convince yourself that this is not a true memory leak by running this code a billion times.

2
  • This is just a small program I created to simulate the problem. In my real program there are a lot of CoCreateInstance calls, and the VM size rises to around 1.5GB, surely this can't be normal... In addition, I can see using perfmon that the process' private bytes are rising, while the .Net CLR Memory bytes in all Heaps doesn't. Also GlowCode can monitor the mananged heap and does not point to a memory leak in the managed part...
    – Oded
    Jan 20, 2011 at 6:15
  • It's not a leak in the managed memory. It's simply an allocation of memory (the CLR is loaded into memory) that isn't later deallocated. But it's not a leak because, by design, the CLR won't be deallocated until the process exits. Jan 20, 2011 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.