Microsoft KB Archive/305823

= PRB: COM+ Instance Count Does Not Decrease When Called from .NET Application =

Article ID: 305823

Article Last Modified on 12/5/2007

-

APPLIES TO


 * Microsoft Enterprise Services (included with the .NET Framework) 1.0
 * Microsoft COM+ 1.5
 * Microsoft COM+ 1.0
 * Microsoft Transaction Services 2.0

-



This article was previously published under Q305823



SYMPTOMS
When you call a COM+ or Microsoft Transaction Server (MTS) component from a .NET application, the object activates in the COM+ or MTS-hosted environment, but when the call completes, the object count does not decrease immediately. This is true even if the object is explicitly set to nothing (Visual Basic .NET) or null (C#) in the .NET client code.



CAUSE
This behavior can occur because the .NET application actually contains a reference to a wrapper, known as a runtime callable wrapper (RCW), which is responsible for managing reference counts on the COM+ or MTS component. This RCW is subject to .NET garbage collection semantics. Only when the RCW is &quot;garbage collected&quot; does it call the final release on the Component Object Model (COM) object. Garbage collection occurs only under memory pressure. Therefore, under simple test conditions, it is possible to observe an object activation remain alive indefinitely in Status view in COM+ Explorer.



RESOLUTION
To resolve this issue, explicitly release the COM object by calling the following method on the RCW from your .NET application: System.Runtime.InteropServices.Marshal.ReleaseComObject(objref) This method call forces the wrapper to release the reference. The wrapper is freed up when garbage collection occurs.



STATUS
This behavior is by design.

