FIX: Automation Object Not Being Released After Call To ComLib.release()

ID: Q218595


The information in this article applies to:
  • Microsoft virtual machine Build 3165 and later
  • Microsoft Visual J++, versions 1.1, 6.0
  • Microsoft SDK for Java, versions 2.0, 2.01, 2.02, 3.0, 3.0 Preview 1, 3.1


SYMPTOMS

When automating an Office product as an out-of-proc server, such as Excel or Word, even though the automation object has been released (through the use of ComLib.release()), its resources may not be freed and the Office product process may not exit after the Java program has exited. For more details on ComLib.release(), see the following article in the Microsoft Knowledge Base:

Q179062 PRB: COM Objects Are Not Being Released in Java


CAUSE

The actual proxy Interface pointer is on another virtual machine thread, waiting to be released, and may not get enough CPU time to make the call.


RESOLUTION

In some cases on more recent builds of the Microsoft virtual machine (Microsoft VM), this workaround will fix the problem. Make a call to ComLib.release as usual, but then also include a call to System.gc() and put the thread to sleep long enough for the object to be released. This problem has been fixed in build 3167 of the Microsoft VM.


STATUS

This bug was corrected in the Microsoft virtual machine version 3167. Build 3167 is the build associated with the SDK for Java version 3.2. The Visual Studio Service Pack 3 also contains this fix in build 3176 of the Microsoft VM.


MORE INFORMATION

Steps to Reproduce Behavior

  • Compile and run the code listed below.


  • Check the running processes list in the Task Manager by pressing CTRL+ALT+DEL.


  • If the problem occurs, there will still be an Excel.exe process listed after the Java application has exited.


  • Note that you will either need to add a COM wrapper in Visual J++ 6.0, or run JactiveX on the Excel object library to generate the wrapper classes required to run this sample. For more information on automating Excel, see the following article in the Microsoft Knowledge Base:
    Q169796 HOWTO: Automate Excel from Java



import excel8.*;
import com.ms.com.*;
import com.ms.win32.*;

public class JExcel
{
  _Application app = null;
  _Global global = null;
  Workbooks workbooks = null;
  _Workbook workbook = null;
  Sheets sheets = null;
  _Worksheet worksheet = null;
  String name = null;
  Variant vNoParam = null;
  Range range = null;
  
  int index = 0;

  public static void main(String args[])
  {
    new JExcel().go();
  }

  public void go()
  {
    try 
    {
      global = (_Global) new Global();	
      app = (_Application)global.getApplication();
      app.setVisible(0, true);

      workbooks = app.getWorkbooks();
      
      vNoParam = new Variant();
      vNoParam.noParam();
      workbook = workbooks.Add(vNoParam, 0);

      name = workbook.getName();
      User32.MessageBox(0, "Workbook Name:  " + name,
                        "Application Message" , 
                        0);
      
      sheets = workbook.getWorksheets();
      
      index = sheets.getCount();
      User32.MessageBox(0, 
                        "Number of sheets:  " + index, 
                        "Application Message" , 
                        0);
      
      for (int i = 1; i <= index; i++)
      {
        worksheet = (_Worksheet)sheets.getItem(new Variant(i));
        worksheet.Activate(0);
        worksheet.setName("Bob's Sheet "+ i);
      }
      
      User32.MessageBox(0, 
                        "Changed sheets' names.", 
                        "Application Message" , 
                        0);
      
      range = worksheet.getColumns();
      range.setColumnWidth(new Variant(40));
      
      User32.MessageBox(0, 
                        "Set column width to 40.", 
                        "Application Message" , 
                        0);
      
      range = worksheet.getCells();
      
      range.setItem(new Variant(1), 
                    new Variant(1), 
                    new Variant("This is a result of a call to Range.setItem"));
      
      User32.MessageBox(0, 
                        "Set the value of cell 1:A.", 
                        "Application Message" , 
                        0);
      
      User32.MessageBox(0, 
                        "Closing Excel Application.", 
                        "Application Message" , 
                        0);
      app.Quit();

      ComLib.release(app);
      ComLib.release(global);
      ComLib.release(workbooks);
      ComLib.release(workbook);
      ComLib.release(sheets);
      ComLib.release(worksheet);
      ComLib.release(name);
      ComLib.release(range);
      //      System.gc();
      //      Thread.currentThread().sleep(1000);
    }
    
    catch(Throwable t)
    {
      User32.MessageBox(0, 
                        t.toString(), 
                        "Exception", 
                        0);
    }
  }
} 


REFERENCES

For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, please see the following pages on the Microsoft Technical Support site:

http://support.microsoft.com/support/visualj/

http://support.microsoft.com/support/java/

The lastest version of the Microsoft virtual machine can be found on the Developer Updates page. For more information on known issues in the SDK for Java version 3.2, please consult the SDK for Java 3.2 release notes. For more information on Visual J++ fixes included in Service Pack 3, please consult the Visual J++ Fixes page.

© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Robert LaCasse, Microsoft Corporation

Additional query words:

Keywords : kbCOMt kbJavaVM kbSDKJava kbVJ600fix kbGrpJava kbVJ600FAQ kbSDKJavaFAQ kbJavaVMFAQ
Version : WINDOWS:1.1,2.0,2.01,2.02,3.0,3.0 Preview 1,3.1,6.0,Build 3165 and later
Platform : WINDOWS
Issue type : kbbug


Last Reviewed: November 16, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.