Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COM Objects and Memory Management

Status
Not open for further replies.

hilfy

Active member
Oct 31, 2003
2,564
US
I'm working with a client who has an application that uses a large number of COM objects which are unmanaged. The specific COM interface that is used is provided by a third-party so its code cannot be modified. This application is occasionally running into "Out of Memory" errors. In the application, the COM objects are instantiated but are never "cleaned up" to release the memory that they use - since a lot of this is done in loops to perform various tasks multiple times I believe that this is what is eating up the memory.

To alleviate the issue I would like to create a set of sub-classes that inherit from both the COM class and IDisposable so that I can implement the Dispose method to free the memory. This would also enable us to us the "Using" syntax to automatically dispose of objects as they pass out of scope. However, I'm having a hard time finding any documentation that specifically identifies what needs to happen in Dispose to clear the object from memory. Can anyone provide me with some sample C# code for this?

Thanks!

-Dell

DecisionFirst Technologies - Six-time SAP BusinessObjects Solution Partner of the Year
 
Hi. This may be a rather late reply, but I hope you can get some good info anyway.

You can browse through the samples from this link:

You may have done this but you may notice that what you needed to do first is to create a COM wrapper, or a proxy, to consume the COM object as if a .NET programmable object. (You can't directly "inherit" the COM type as it's not CLR). And you may have already realized that this is also what VS will produce when you try to add a COM reference to your .NET project: an Interop DLL.

I cannot remember how the COM object is instantiated inside the Interop dll (thru Marshal class?), but I do know that one of the ways you can create a COM instance is via the Activator class. And how ever you instantiate a COM object, you will always get a runtime callable wrapper (RCW), often you only see it as System.Object type. This is "the" object that you need to dispose, and you can do it via the Marshal.ReleaseComObject(). This would dispose, so to speak, the COM object, but its not a guarantee it will solve the memo leak issue since we do not know if the COM object itself is properly deallocating whatever resources it used.

hth [wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top