Microsoft KB Archive/97028

= Microsoft Knowledge Base =

BUG: Repeated XFileCmp Calls Leave Script Without File Handles
Last reviewed: February 24, 1995

Article ID: Q97028

The information in this article applies to:

- Microsoft Test for Windows, version 1.0

SYMPTOMS
The FastTest routine XFileCmp does not release the file handles it uses to open the two comparison files if an error occurs upon opening, reading, or closing one of those files. Because of this, if you make repeated calls to XFileCmp, your script may eventually be left without any file handles to work with.

WORKAROUND
You have the source code to FastText. To correct the problem, make the following corrections to the code.

Add code to XFileCmp to close any open file handles within each error-checking condition. To add the code to correct this problem, load the FastTest file FTESTUTL.MST into TestDriver and make the following changes to the XFileCmp routine:

  Add the following lines at the end of the SUB, just before the END SUB statement: EXIT SUB CloseFile: If fh1% Then Close fh1% End If    If fh2% Then Close fh2% End If    RETURN   Add the line &quot;GOSUB CloseFile&quot; above each call to EXIT SUB found in the IF THEN blocks that handle errors. This ensures that all file handles are closed prior to exiting from the procedure. The code below has been modified as explained above -- modifications are marked with a '*** comment: 

SUB XFileCmp(stFileSpec1$,stFileSpec2$) STATIC

DIM fh1%   ' file handle of first file DIM fh2%   ' file handle of second file DIM line1$ ' line from first file DIM line2$ ' line from second file DIM done   ' flag to stop looping DIM diff   ' flag to indicate if files compare

gErrorType = ET_NEXT fh1% = FREEFILE OPEN stFileSpec1$ FOR INPUT AS #fh1% fh2% = FREEFILE OPEN stFileSpec2$ FOR INPUT AS #fh2%

IF gfError THEN XLogFailure &quot;Could not open files for XFileCmp&quot; gErrorType = ET_NOTHING gfError = FALSE GOSUB CloseFile  ' *** Added to Close Files EXIT SUB END IF

done = FALSE diff = FALSE

IF EOF(fh1%) AND EOF(fh2%) THEN done = TRUE ELSEIF EOF(fh1%) OR EOF(fh2%) THEN diff = TRUE done = TRUE END IF

WHILE NOT done LINE INPUT #fh1%,line1$ LINE INPUT #fh2%,line2$

IF gfError THEN XLogFailure &quot;XFileCmp INPUT or EOF errors&quot; gErrorType = ET_NOTHING gfError = FALSE GOSUB CloseFile  ' *** Added to Close Files EXIT SUB END IF       IF line1$ <> line2$ THEN done = TRUE diff = TRUE END IF       IF NOT done AND EOF(fh1%) AND EOF(fh2%) THEN done = TRUE END IF       IF NOT done AND (EOF(fh1%) OR EOF(fh2%)) THEN diff = TRUE done = TRUE END IF   WEND

CLOSE #fh1% CLOSE #fh2%

IF gfError THEN XLogFailure &quot;XFileCmp CLOSE errors&quot; gErrorType = ET_NOTHING gfError = FALSE GOSUB CloseFile  ' *** Added to Close Files EXIT SUB END IF

gErrorType = ET_NOTHING

IF diff THEN 'Enter the following on one, single line: XLogFailure &quot;Files &quot; + stFileSpec1$ + &quot;,&quot; + stFileSpec2$ + &quot;don't compare&quot; END IF

'*** Code added to ensure file handles are closed *** Exit Sub '*** Exit if no errors occurred CloseFile:

If fh1% Then Close fh1% End If       If fh2% Then Close fh2% End If       RETURN END SUB

STATUS
Microsoft has confirmed this to be a bug in Microsoft Test version 1.0 for Windows. Because the source code for FastTest is provided with the product, you can use the workaround shown above to modify the code and correct the problem and close any file handles used in the XFileCmp routine.

Steps to Reproduce Problem
The example shown below relies on the function GetFreeFileHandles (part of the STRESS.DLL file that comes with the Microsoft Windows Software Development Kit (SDK)) because Microsoft Test cannot open more than five files at once.

 Start Microsoft Test or from the File menu, choose New (ALT, F, N) if Microsoft Test is already running.  Enter the following code into the newly created script: Declare Function GetFreeFileHandles Lib &quot;STRESS.DLL&quot; As Integer '$INCLUDE 'FASTTEST.INC'

Viewport Clear XSetTerminate False

Print &quot;Initial Number of Free Handles = &quot;;GetFreeFileHandles

file1$ = &quot;Test1.dat&quot; ' Modify to a filename that doesn't exist. file2$ = &quot;Test2.dat&quot;

xfilecmp file1$, File2$

Print &quot;Final Number of Free Handles = &quot;;GetFreeFileHandles  Press the F5 key to run the script.

The number of initial and final free file handles displayed in the ViewPort shows there is one less file handle available after the call to XFileCmp. The two numbers should be equal.