Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V6115. Not all Closeable members are re…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
Problems related to code analyzer
Additional information
toggle menu Contents

V6115. Not all Closeable members are released inside the 'close' method.

Jul 10 2024

The analyzer has detected fields (resources) in the class implementing the 'Closeable' (or 'AutoCloseable') interface. The fields also implement this interface but are not released in the 'close' method of the class.

class A implements Closeable {

  private FileWriter resource;
 
  public A(String name) {
    resource = new FileWriter(name); 
  }

  public void close() {
    // resource is not released
  }
}

In this example, a developer initializes the 'resource' field but does not call the 'close' method within the 'A' class. The absence of the 'close' method results in the resource not being released even when the 'close' method is called for an object of the 'A' class. A program logic error may occur. For example, if a resource is not released, it cannot be accessed from another part of the code.

Such an error may persist even if the resource is closed in one of the methods:

class A implements Closeable {

  private FileWriter resource;

  public A(String name) {
    resource = new FileWriter(name); 
  }

  public void endWrite() {
    resource.close();
  }

  public void close() {
    // resource is not released, the endWrite method is not called 
  }
}

We can fix it in several ways. One of them is to release the resource inside the 'close' method of the class:

class A implements Closeable {

  private FileWriter resource;

  public A(String name) {
    resource = new FileWriter(name); 
  }

  public void close() {
    resource.close();
  }
}

Another option to fix it is to add the method call that closes the resource in the 'close' method:

class A implements Closeable {

  private FileWriter resource;

  public A(String name) {
    resource = new FileWriter(name); 
  }

  public void endWrite() {
    resource.close();
  }

  public void close() {
    endWrite(); 
  }
}

This diagnostic is classified as:

close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam