ANDROID.UF.CAMERA

UF (Use Freed) issues are reported when there is an attempt to use resources after they were released. The ANDROID.UF.CAMERA warning indicates an attempt to use Camera after it was released.

Example 1

Copy
     private Camera camera;
 
     private void initCamera() {
         camera = Camera.open();
     }
 
     public boolean onKeyDown(final int keyCode, final KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
             if (camera != null) {
                 final CameraPictureCallbackImpl callback = new CameraPictureCallbackImpl();
                 camera.takePicture(null, null, callback);
             }
             return true;
         }
         return super.onKeyDown(keyCode, event);
     }
 
     private class CameraPictureCallbackImpl implements Camera.PictureCallback {
         private Bitmap bitmap;
 
         public void onPictureTaken(final byte[] bytes, final Camera camera) {
             camera.stopPreview();
             camera.release();
             bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
             if (bitmap == null) {
                 camera.startPreview();
             }
         }
 
         public Bitmap getBitmap() {
             return bitmap;
         }
     }

ANDROID.UF.CAMERA is reported for the snippet on line 46 since callback method 'onPictureTaken(...)' starts the camera preview even if the camera was released on line 43.

Extension

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