Microsoft KB Archive/32539

= PRB: fclose on Unopened Files Causes Protection Violation =

Article ID: 32539

Article Last Modified on 12/11/2003

-

APPLIES TO

 The C Run-Time (CRT), when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 5.0 Standard Edition</li></ul>

 Microsoft Visual C++ 6.0 Service Pack 5</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q32539

<div class="notice_section">

<div class="symptoms_section">

SYMPTOMS
An attempt to close a file with the fclose function fails and one of the following occurs:
 * A protection violation or application error occurs
 * A &quot;R6001 - null pointer assignment&quot; error occurs

<div class="cause_section">

CAUSE
The specified file was not open.

16-bit applications: The source code is compiled in the compact or large memory models. In the small memory model, fclose may return EOF as expected or it may generate an error cited above.

<div class="resolution_section">

RESOLUTION
Modify the source code to only close files that are already open.

<div class="moreinformation_section">

MORE INFORMATION
An attempt to close a file that is not open is a user error. The C run- time library does not verify the validity of file handles passed to the fclose function.

The code example below demonstrates this behavior.

Sample Code
/* * Compile options needed - 16-bit: /AL *                       - 32-bit: none */

FILE *myfile; int status;
 * 1) include <stdio.h>

main {  myfile = fopen(&quot;myfile.dat&quot;, &quot;r&quot;); if (myfile == NULL) printf(&quot;file open error\n&quot;); status = fclose(myfile); printf(&quot;file close status = %d\n&quot;, status); }

Keywords: kbcrt kbprb KB32539

-

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

© Microsoft Corporation. All rights reserved.