Hello,
I'm trying to connect a Delphi COM client with our .NET server. Everything
seems to work fine, except that COM-created .NET objects are not properly
garbage collected, e.g. the C# destructors won’t get invoked.
Especially when marshalling .NET streams to COM IStreams which do not
provide a close or dispose mechanism this behaviour causes many problems.
Although trying hard I couldn’t find any similar problem descriptions in the
internet. Are we doing something wrong or is it a fact that COM-created .NET
instances won’t get finalized? And is there any kind of best practice how to
deal with that problem?
Thanks for your ideas,
Guido
"Peter Huang" [MSFT] - 21 Jun 2005 07:39 GMT
Hi
Based on my experience, the COM client -->CCW-->.NET code.
But the CCW will not call the dispose on the .NET library automatically,
also the Finalizer queue(i.e. the deconstructor of .NET class) is managed
by GC, it took the responsibility to handle the release issue.
So for now, I suggest you implement the IDisposable interface on your .net
class and call the dispose explicitly in your com client to do the release
the necessary resource.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace DecntorComServer
{
[ClassInterface(ClassInterfaceType.AutoDual)]
public class MyClass : IDisposable
{
public MyClass()
{
Debug.WriteLine("MyClass Constructor");
}
public void TestCall()
{
Debug.WriteLine("Call in");
}
~MyClass()
{
Debug.WriteLine("MyClass DeConstructor");
//We will see that even use the class in managed code, we still need to
call GC.Collect() to enable the Finalize/deconstructor
//Also we did not recommend call GC.Collect ourselves, because that may
lead to performance hit.
}
#region IDisposable ³ÉÔ±
public void Dispose()
{
Debug.WriteLine("Calling Dispose");
}
#endregion
}
}
Best regards,
Peter Huang
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.