JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
A resource is not closed by the end of the method in which it is created.
Resources such as files or database connections should be closed, or otherwise the system might run into an out of resource exception. The close operation should always be performed, in every execution path. For this reason, the try-with-resource or finally constructs should be used.
This checker verifies that resources are always closed after being open, for every execution path. This checker recognizes and accepts as correct code where a resource is stored into a field or returned from a method and is then closed at the end, at least for some frequent scenarios.
| Class Name | Closeable Not Closed (Java) | ||||||
|---|---|---|---|---|---|---|---|
| Significance | reliability | ||||||
| Mnemonic | JAVA.ALLOC.LEAK.NOTCLOSED | ||||||
| Categories |
|
||||||
| Availability | Available for Java and Kotlin. |
||||||
| Enabling | Checks for this warning class are enabled by
default. To disable them, add the following WARNING_FILTER rule to the
project configuration file.
WARNING_FILTER += discard class="Closeable Not Closed (Java)" |
import java.io.FileWriter;
import java.io.IOException;
public class TestCloseOfResources {
private final FileWriter myOtherField;
public TestCloseOfResources() throws IOException {
myOtherField = new FileWriter("temp.txt"); /* Closeable Not Closed (Java)
* warning issued here - myOtherField should be closed inside a 'finally' block.
* If an exception occurs during the execution myOtherField.write, the program will not be able
* to perform the close at the next line.
*/
myOtherField.write("wo sind die Helden der Vergangenheit?");
myOtherField.close();
}
public TestCloseOfResources(int i) throws IOException {
myOtherField = new FileWriter("temp.txt");
try {
myOtherField.write("wo sind die Helden der Vergangenheit?");
}
finally {
myOtherField.close();
}
}
public TestCloseOfResources(float f) throws IOException {
myOtherField = new FileWriter("temp.txt"); /* Closeable Not Closed (Java)
* warning issued here - myOtherField may not be closed
* if the result of System.currentTimeMillis() is an odd number.
*/
try {
myOtherField.write("wo sind die Helden der Vergangenheit?");
}
finally {
if (System.currentTimeMillis() % 2 == 0)
myOtherField.close();
}
}
public void test3() throws IOException {
FileWriter writer = null;;
try {
writer = new FileWriter("temp.txt");
writer.write("wo sind die Helden der Vergangenheit?");
}
finally {
if (writer != null)
writer.close();
}
}
public void test4() throws IOException {
try (FileWriter writer = new FileWriter("temp.txt")) {
writer.write("wo sind die Helden der Vergangenheit?");
}
}
public FileWriter test16() throws IOException {
return new FileWriter("temp.txt");
}
public void test19() throws IOException {
FileWriter writer = test16();
try {
writer.write("wo sind die Helden der Vergangenheit?");
}
finally {
writer.close();
}
}
public void test99() throws IOException {
new FileWriter("temp.txt").write("wo sind die Helden der Vergangenheit?"); /* Closeable Not Stored (Java)
* warning issued here - the resource is not stored into a local variable so
* there is no way to close it.
*/
}
public void test100() throws IOException {
FileWriter writer = new FileWriter("temp.txt");
try {
writer.write("wo sind die Helden der Vergangenheit?");
}
finally {
writer.close();
}
}
}
Guarantee that all resources are closed, typically by using a try-with-resource or a finally construct. In general, avoid storing resources into fields or returning resources from methods or letting a method close a resource passed to it as a parameter.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.