Rule Definition
When a resource is created or allocated, the developer is responsible for properly releasing the resource as well as accounting for all potential paths of expiration or invalidation, such as a set period of time or revocation
Remediation
The activity
You should always override the onStop() and call the release() as follow :
public class MyActivity extends Activity{
MediaPlayer mMediaPlayer;
...
@Override
public void onStop() {
MR.stop();
MR.release();
MR = null;
}
}
-----
In the case when using a service, you should always override the onDestroy() method to make sure you are releasing the media object:
public class MyService extends Service {
MediaPlayer mMediaPlayer;
...
@Override
public void onDestroy() {
super.onDestroy()
if (mMediaPlayer != null) mMediaPlayer.release();
}
}
Violation Code Sample
Using Media Object as activity
public class MyActivity extends Activity {
private MediaRecorder MR;
@Override
public void onCreate(Bundle state) {
...
}
@Override
public void onRestart() {
...
}
@Override
public void onStop() {
MR.stop();
}
}
-----
Using Media Object as Service
public class MyService extends Service {
MediaPlayer mMediaPlayer;
// ...
@Override
public void onDestroy() {
}
}
Reference
https://cwe.mitre.org/data/definitions/404.html
Related Technologies
Technical Criterion
CWE-404 - Improper Resource Shutdown or Release [Class]
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.