Interop COM in C# - can't get the object released

G

Guest

I am trying to develop an ATL COM object together with a C# web
application that will host it. I am following the COM Interop Part 1: C#
Client Tutorial, Example 2, The C# Code Approach from the April 2004
MSDN library exactly. But the client will not release the COM object.

The COM object is being run in its own solution and under a separate
instance of Visual Studio .NET 2003. At this point the object consists
only of the constructor, FinalConstruct, and FinalRelease. No methods or
properties have been implemented on the interface.

The C# client is the corresponding code according to the tutorial and is
in the code-behind of a .NET web form. No methods or properties on the
interface.

However, after I have run the simple

MyCOMTypeLib.MyCOMManager manager = new MyCOMTypeLib.MyCOMManager()

command and have exited either the debugger or the browser independent
of Visual Studio. A reference remains on MyCOM.dll. The dll cannot be
rebuilt and it cannot be deleted. The only way I have found to clear the
reference is to reboot the computer.

Is there a way for force the DLL to be unloaded? When programming ASP,
this could be done by unloading the application with the IIS manager.

Better yet, why is this reference remaining contrary to documentation?
 
J

John Puopolo

Hi..

Check out the Marshal.ReleaseComObject method. This method ensures that the
COM object is properly decremented so that the COM runtime will perform
proper clean up and unloading of the host (DLL).

John Puopolo
 
G

Guest

John,

Thank you for your response.

I tried that without success.

Stopping and restarting the web service does remove the reference. Not ideal.
 
W

Willy Denoyette [MVP]

Alan said:
John,

Thank you for your response.

I tried that without success.

Stopping and restarting the web service does remove the reference. Not
ideal.

Release the object or release the DLL?
COM DLL's are unloaded when the process calls CoUnintialize, not when the
last reference of a contained object is released. If you need to unload
unused COM DLL's from memory you have to explicitely call
CoFreeUnusedLibraries through PInvoke.


Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top