Microsoft KB Archive/179062

= PRB: COM Objects are not Being Released in Java =

Article ID: 179062

Article Last Modified on 6/14/2006

-

APPLIES TO


 * Microsoft Software Development Kit for Java 1.51
 * Microsoft Software Development Kit for Java 2.02
 * Microsoft Software Development Kit for Java 2.01
 * Microsoft Software Development Kit for Java 2.02
 * Microsoft Software Development Kit for Java 3.0
 * Microsoft Software Development Kit for Java 3.1
 * Microsoft Software Development Kit for Java 3.2
 * Microsoft Visual J++ 1.0 Standard Edition
 * Microsoft Visual J++ 1.1 Standard Edition

-



This article was previously published under Q179062



SYMPTOMS
When setting a COM object reference to null and possibly attempting to force garbage collection by calling gc, COM objects may not be released in a timely or predictable fashion.



CAUSE
If you do not call ComLib.release, normal garbage-collection will attempt to perform the release for you. However, this mechanism is not guaranteed due to the threading limitations of many COM objects. That is, many COM objects can be called only on the thread on which they were created. Because garbage collection occurs at unpredictable times, the required thread may have expired or may be no longer responding to messages by the time garbage-collection reclaims the object. In addition, this unpredictability can obscure true memory leaks and/or tie up important system resources.



RESOLUTION
It is recommended that you use explicit releases by calling ComLib.release in order to free COM objects in a timely and predictable fashion.



MORE INFORMATION
For example, if you have the following code that creates a COM object in Java: import excel8.*; import com.ms.com.*;

_Global globXL=null; _Application appXL=null;

try {  globXL = (_Global)new Global; appXL = (_Application)globXL.getApplication; } catch (ComFailException e) { System.out.println(e.getMessage); } You would use this to release these COM objects (note all references to COM objects should be released): try {  ComLib.release(appXL); appXL=null; ComLib.release(globXL); globXL=null; } catch (ComFailException e) { System.out.println(e.getMessage); }

