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

ANDROID.RLK.MEDIARECORDER

RLK (Resource Leak) issues are reported when resources are allocated but not properly disposed after use. An ANDROID.RLK.MEDIARECORDER warning indicates that a MediaRecorder that was opened is not explicitly released.

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

20     public boolean onKeyDown(final int keyCode, final KeyEvent event) {
21         if (keyCode == KeyEvent.KEYCODE_ENTER) {
22             MediaRecorder recorder = new MediaRecorder();
23             recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
24             recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
25             recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
26             recorder.setOutputFile(PATH_NAME);
27             recorder.prepare();
28             recorder.start();   // Recording is now started
29             recorder.stop();
30             recorder.reset();   // You can reuse the object by going back to setAudioSource() step
31             recorder.release();
32             return true;
33         }
34         return super.onKeyDown(keyCode, event);
35     }

ANDROID.RLK.MEDIARECORDER is reported for the snippet on line 22: 'recorder' will not be released on exit if 'setAudioSource(...)' throws java.lang.IllegalStateException (line 23).

Example 2

20     public boolean onKeyDown(final int keyCode, final KeyEvent event) {
21         if (keyCode == KeyEvent.KEYCODE_ENTER) {
22             MediaRecorder recorder = new MediaRecorder();
23             try {
24                 recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
25                 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
26                 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
27                 recorder.setOutputFile(PATH_NAME);
28                 recorder.prepare();
29                 recorder.start();   // Recording is now started
30                 recorder.stop();
31                 recorder.reset();   // You can reuse the object by going back to setAudioSource() step
32             } finally {
33                 recorder.release();
34             }
35             return true;
36         }
37         return super.onKeyDown(keyCode, event);
38     }

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

Extension

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