Applies To:
  • CitectSCADA 5.xx, 6.00
  • CitectHMI 5.xx, 6.00

Summary:
A file handle leak in CitectSCADA cicode can be easily caused by incorrectly using FileFind() with wildcard characters to find a list of files.
 

Solution:
The CitectSCADA help for FileFind() gives the following explanation of how to find a list of files:

"To find a list of files, you must first call this function with the required path and mode (to find the first file), then call the function again with an empty path and a mode of 0 (to find the remaining files). After the last file is found, an empty string is returned."

It also gives the following example to demonstrate this:

! Search for all dBase files in the run directory and make a backup

sPath = FileFind("[run]:\*.dbf", 0);

WHILE StrLength(sPath) > 0 DO

        FileSplitPath(sPath, sDrive, sDir, sFile, sExt);

        sBak = FileMakePath(sDrive, sDir, sFile, "BAK");

        FileCopy(sPath, sBak, 0);

        ! Find the next file

        sPath = FileFind("", 0);

END


A common pitfall is to call sPath = FileFind("[run]:\*.dbf", 0) within the while loop. That is turn the above example code into:

! Search for all dBase files in the run directory and make a backup

sPath = FileFind("[run]:\*.dbf", 0);

WHILE StrLength(sPath) > 0 DO

        FileSplitPath(sPath, sDrive, sDir, sFile, sExt);

        sBak = FileMakePath(sDrive, sDir, sFile, "BAK");

        FileCopy(sPath, sBak, 0);

        ! Find the next file

        sPath = FileFind("[run]:\*.dbf", 0); // Handle leak!

END


This in turn will create a file handle leak which could lead to CitectSCADA crashes and system instability. The number of object handles being used by CitectSCADA can be viewed within the Windows Task Manager, when the Process tab is active select the View >> Select Columns menu item. Then selecting the "handle count" checkbox will show this information for Citect32.exe. A file handle leak will lead to a "handle count" value that increases continuously.

 

Keywords:
FileFind, FileFind(), file handle leak 
 

Attachments