Home | Overview | How Do I | FAQ
During termination-handler execution, you may not know which resources are actually allocated before the termination handler was called. It is possible that the __try statement block was interrupted before all resources were allocated, so that not all resources were opened.
Therefore, to be safe, you should check to see which resources are actually open before proceeding with termination-handling cleanup. A recommended procedure is to:
For example, the following code uses a termination handler to close three files and a memory block that were allocated in the __try statement block. Before cleaning up a resource, the code first checks to see if the resource was allocated.
void FileOps()
{
FILE *fp1, *fp2, *fp3;
LPVOID lpvoid;
lpvoid = fp1 = fp2 = fp3 = NULL;
__try {
lpvoid = malloc( BUFFERSIZE );
fp1 = fopen( "ADDRESS.DAT", "w+" );
fp2 = fopen( "NAMES.DAT", "w+" );
fp3 = fopen( "CARS.DAT", "w+" );
.
.
.
}
__finally {
if ( fp1 ) fclose( fp1 );
if ( fp2 ) fclose( fp2 );
if ( fp3 ) fclose( fp3 );
if ( lpvoid ) free( lpvoid );
}
}