Reputation: 26159
I have written a multi thread unmanaged application which uses COM objects in its worker threads. Everything went fine until I started using .NET objects exported as COM to do the work.
Playing around with the code and commenting out parts of the .NET object functionality I managed to pin it down to the usage of COM objects from within the .NET objects. To summarize:
To my surprise the memory consumption of the application started climbing up steadily until OutOfMemory exception started to appear.
Here is my .NET implementation:
void DoSomeWork()
{
IComObject O = new ComObjectClass();
try
{
// do something
}
finally
{
Marshal.ReleaseComObject(O);
}
}
If I comment out this function, the memory leaks disappear. If I call GC.Collect() in it the memory leak still happens.
Any ideas as to what may be going on?
EDIT: Some more info based on comments and answers:
Upvotes: 0
Views: 2470
Reputation: 430
Hi would suggest you use following peace of code.
if (feature != null)
{
while (Marshal.ReleaseComObject(feature) > 0)
{ }
feature=null;
}
if you are using com object as reference type. ex. cursor. If you are simply doing Marshal.ReleaseComObject(O);
Than it would free only one reference and rest of the reference would be kept inside the memory so better to free all the references.
Upvotes: 0
Reputation: 116471
It could be that your COM wrapper objects need to switch to a STA thread to finalize. If your STA doesn't pump messages this will never happen and thus the finalize will never be called which in turn causes the memory leak.
There's more info in this KB.
Upvotes: 2