Microsoft KB Archive/279777

= BUG: FOR EACH Reference Fails To Release Automation Server =

Article ID: 279777

Article Last Modified on 10/16/2002

-

APPLIES TO


 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition

-



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(&quot;A1:D4,E5:H8&quot;)

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



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



Steps to Reproduce Behavior
To see this behavior with Microsoft Excel and Microsoft Windows NT and Microsoft Windows 2000, perform the following steps:  Open the Task Manager, and confirm that no instance of Excel is currently running.  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  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.

Keywords: kbbug kbautomation kbpending KB279777

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.