Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

COM Objects and Memory Management

COM Objects and Memory Management

COM Objects and Memory Management

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?



DecisionFirst Technologies - Six-time SAP BusinessObjects Solution Partner of the Year

Replies continue below

Recommended for you

RE: COM Objects and Memory Management

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close