Resource leak Medium

Allocated resources are not released properly. This can slow down or crash your system. They must be closed along all paths to prevent a resource leak.

Detector ID
Common Weakness Enumeration (CWE) external icon

Noncompliant example

1def read_file_noncompliant(filename):
2    file = open(filename, 'r')
3    # Noncompliant: method returns without properly closing the file.
4    return file.readlines()

Compliant example

1def read_file_compliant(filename):
2    # Compliant: file is declared using a `with` statement.
3    with open(filename, 'r') as file:
4        return file.readlines()