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

Copy
     public boolean onKeyDown(final int keyCode, final KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_ENTER) {
             MediaRecorder recorder = new MediaRecorder();
             recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
             recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
             recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
             recorder.setOutputFile(PATH_NAME);
             recorder.prepare();
             recorder.start();   // Recording is now started
             recorder.stop();
             recorder.reset();   // You can reuse the object by going back to setAudioSource() step
             recorder.release();
             return true;
         }
         return super.onKeyDown(keyCode, event);
     }

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

Copy
     public boolean onKeyDown(final int keyCode, final KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_ENTER) {
             MediaRecorder recorder = new MediaRecorder();
             try {
                 recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                 recorder.setOutputFile(PATH_NAME);
                 recorder.prepare();
                 recorder.start();   // Recording is now started
                 recorder.stop();
                 recorder.reset();   // You can reuse the object by going back to setAudioSource() step
             } finally {
                 recorder.release();
             }
             return true;
         }
         return super.onKeyDown(keyCode, event);
     }

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.