BUG: FOR EACH Reference Fails To Release Automation Server (279777)



The information in this article applies to:

  • Microsoft Visual FoxPro for Windows 5.0
  • Microsoft Visual FoxPro for Windows 5.0a
  • Microsoft Visual FoxPro for Windows 6.0

This article was previously published under Q279777

SYMPTOMS

If you use a FOR EACH loop to iterate through a collection of objects in an Automation Server, the server may not release correctly when you release the variables.

RESOLUTION

To resolve this problem, use the CLEAR ALL command to clear all memory, including any hidden references. This may not be an optimal solution, however, because you may be depending on variables that should remain in memory, such as an application object.

Another possible resolution is to find other ways to refer to the collection. For example, given the following code, you want to set a value for each cell in a range:
loRange = loWorkbook.ActiveSheet.Range("A1:D4,E5:H8")

*!* This loop will show the bug.
FOR EACH loCell in loRange
   loCell.Value = "Fred"
ENDfor
      
*!* This single command will not.
loRange.Value = "Fred"
				

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce Behavior

To see this behavior with Microsoft Excel and Microsoft Windows NT and Microsoft Windows 2000, perform the following steps:
  1. Open the Task Manager, and confirm that no instance of Excel is currently running.
  2. Open Visual FoxPro, and paste the following code into a .prg file:
    LOCAL loExcel, lcFile, loWorkbook, lcRange, loCell, loRange
    
    lcFile = GETFILE('XLS')
    
    IF NOT EMPTY(lcFile)
       loExcel = CREATEOBJECT('Excel.Application')
       loExcel.Visible = .t.
    
       loWorkbook = loExcel.Workbooks.Open(lcfile)
       lcRange = loExcel.Selection.Address(.F., .F., 1)
       loRange = loWorkbook.ActiveSheet.Range(lcRange)
    
       FOR EACH loCell IN loRange
          *!* Empty loop
       NEXT
    
       loCell = .NULL.
       loRange = .NULL.
    
       loExcel.Quit()
    ENDif
    					
  3. Run the program, and then select an Excel file.
Note that although the Excel window goes away, an instance of Excel.exe is left in the Task Manager. If you comment out the FOR EACH loop, the instance releases properly.

Modification Type:MajorLast Reviewed:10/16/2002
Keywords:kbAutomation kbBug kbDSupport KB279777