Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

RLK.OUT

RLK (Resource Leak) issues are reported when resources are allocated but not properly disposed after use. Failing to properly dispose a resource can lead to such problems as:

  • too many files being open
  • an application not being able to access a temporary file when it is needed

RLK.OUT indicates that an output stream that was opened is not explicitly closed.

Vulnerability and risk

Resources such as streams, connections and graphic objects must be explicitly closed. The close operation can unblock transactions or flush file changes in the file system. While a resource will eventually be closed by the garbage collector, resource exhaustion can occur before garbage collection starts. Depending on the nature of the resource, various exceptions will be thrown on a failed attempt to allocate another resource, for example: java.io.FileNotFoundException: Too many open files or too many database connections.

Mitigation and prevention

Explicitly close all resources that have the close method, even those that you think are not doing anything significant. Future code changes will then be safe from such errors.

Example 1

11     public void printLines(String fName, Collection<String> lines) {
12         try {
13             File file = new File(fName);
14             PrintWriter pstr = new PrintWriter(
15                     new BufferedOutputStream(
16                             new FileOutputStream(file)));
17             for (final String line : lines) {
18                 pstr.println(line);
19             }
20         } catch (IOException e) {
21             e.printStackTrace();
22         }
23     }

RLK.OUT is reported for the snippet on line 16: Output stream 'pstr' is not closed on exit.

Example 2

11    public void printLines(String fName, Collection<String> lines) {
12         try {
13             File file = new File(fName);
14             PrintWriter pstr = new PrintWriter(
15                     new BufferedOutputStream(
16                             new FileOutputStream(file)));
17             try {
18                 for (final String line : lines) {
19                     pstr.println(line);
20                 }
21             } finally {
22                 pstr.close();
23             }
24         } catch (IOException e) {
25             e.printStackTrace();
26         }
27     }

The snippet from the previous section is fixed; RLK.OUT is not reported here.

Extension

This checker can be extended through the Klocwork knowledge base. See Tuning Java analysis for more information.