Open file handle leak

A file was opened but never closed.

When a file is opened, a set of resources are acquired such as input and output data buffers. If the file is not closed, these resources are never released. Furthermore, the operating system imposes some maximum limit on the number of files that can be open simultaneously. If this limit is exceeded, subsequent open attempts will fail. For this reason files should always be closed when they are no longer needed, especially in long running processes.

ID

Observation

Description

1

Resource allocation

The place the file was opened

2

Resource leak

The place the file handle was leaked

Examples

          
#include <stdio.h> 

int main(int argc, char **argv)
{
    int num_open_files = -1;
    char file_name[20] = "file";
    FILE *fp;
    do {
        sprintf(file_name + 5, "%08d", num_open_files++);
        fp = fopen(file_name, "w");
    } while (fp != 0);
    printf("Maximum number of open files = %d\n", num_open_files);
}
        

Copyright © 2010, Intel Corporation. All rights reserved.