Strange Problem with releasing bitmaps and 'back' button

All your problems with Audio, Video and Images.

Strange Problem with releasing bitmaps and 'back' button

Postby » Sun Dec 05, 2010 8:12 pm


This would be hard to explain so I'll try to make it as simple as it can be. Also don't know if it is in fact a multimedia issue - more like a life cycle thing.

I have an app that consist 2 activities. In the first one I have 2 animations with bitmaps.When I used the google suggested approach for animationdrawable (with xml file) I was constantly having OOME. Only using Bitmaps which I can release manually solved the problem, but not quite. For clarity I'll describe this in points:

1. On launch I prepare everything, load the first animation and start a timer:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. analyseBitmaps[0] = BitmapFactory.decodeResource(res, R.drawable.analyse_frame_0);
  2. analyseBitmaps[1] = BitmapFactory.decodeResource(res, R.drawable.analyse_frame_1);
  3. analyseBitmaps[2] = BitmapFactory.decodeResource(res, R.drawable.analyse_frame_2);
  4. analyseBitmaps[3] = BitmapFactory.decodeResource(res, R.drawable.analyse_frame_3);
  5. analyseBitmaps[4] = BitmapFactory.decodeResource(res, R.drawable.analyse_frame_4);
  6. analyseBitmaps[5] = BitmapFactory.decodeResource(res, R.drawable.analyse_frame_5);
  7. analyseBitmaps[6] = BitmapFactory.decodeResource(res, R.drawable.analyse_frame_6);
  9. for (int i =0 ; i< analyseBitmaps.length ; i++)
  10. {
  11.    analyseAnimation.addFrame(new BitmapDrawable( analyseBitmaps[i] ), 50);
  12. }
  13. analyseAnimation.setOneShot(false);
  14. animationImage.setBackgroundDrawable(analyseAnimation);
  15. analyseAnimation.start();
  18. MyCount counter = new MyCount(15000,1000);
  19. counter.start();
Parsed in 0.014 seconds, using GeSHi

2. Then after the timer comes to an end I release the first animation bitmaps and load the second one in an identical way. Then I do some long-time data processing and connect to the internet in a separate thread. Here is how I release the first one:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. for (int i =0 ; i< analyseBitmaps.length ; i++)
  2. {
  3.    if ( analyseBitmaps[i] != null )
  4.    {
  5.            if ( !analyseBitmaps[i].isRecycled())
  6.                    analyseBitmaps[i].recycle();
  7.            if (analyseBitmaps[i].isRecycled())
  8.                    System.out.println("Destroyed analyseBitmaps nr: "+i);
  9.    }
  10. }
Parsed in 0.010 seconds, using GeSHi

I need to load them one-by-one because if I load both of them at the beggining I have an OOME.

3. After the processing is over I finish this activity and launch a 2nd one to view the results. I have Overrided the onDestroy() method to release the bitmaps:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. public void onDestroy()
  3. {
  4.         super.onDestroy();                     
  6.    for (int i =0 ; i< analyseBitmaps.length ; i++)
  7.    {
  8.            if ( !analyseBitmaps[i].isRecycled())
  9.                    analyseBitmaps[i].recycle();
  10.            if (analyseBitmaps[i].isRecycled())
  11.                    System.out.println("Destroyed analyseBitmaps nr: "+i);
  12.    }   
  14.  for (int i =0 ; i< connectingBitmaps.length ; i++)
  15.    {
  16.            if ( !connectingBitmaps[i].isRecycled())
  17.                    connectingBitmaps[i].recycle();
  19.            if (connectingBitmaps[i].isRecycled())
  20.                            System.out.println("Destroyed connectingBitmaps nr: "+i);
  21.    }
  23.    animationImage.setBackgroundResource(0);
  25. }
Parsed in 0.011 seconds, using GeSHi

4. The second activity show the results and has a button that gets back to the first activity.

Now what is the problem. If I launch the app and keep it working normally everything is doing fine. I can keep the activity going on and on and on - no memory leaks. Until I have pressed the 'back' button while the 1 animation was working, and came to the app again and I had an OOME. What I figured out is that my OnDestroy method would delete the analyseBitmaps (and only them) but the app keeps on working, launches the 2nd animation and after finishing the work shows the 2nd activity. So the connectingBitmaps are not released in onDestroy (because they are not loaded yet) and they cause the leak. I need to find a way to release connectingBitmaps after the 1st activity stops working(despite being destroyed already) in the OS background. I can't release them just at the end of the 1st activity before calling finish() because I have this exception:
java.lang.RuntimeException: Canvas: trying to use a recycled bitmap

So my questions are:
1. How to cover this situation so that the 2nd animation would be destroyed when the 'back' button is pressed?
2. I can imagine a situation when the user launches the app, and accidentally presses the back button. Then he want to immediately relaunch the app again. So 2 instance of the app would be working at the same time. I tried that and it leaded to super-slow work and eventual error. How to cover this?

This is sth. I can't quite comprehend and couldn't get any clue on google, which seems odd to me because sb. must have run into similar issues with 'back' button. Can any one explain this?
Junior Developer
Junior Developer
Posts: 22
Joined: Wed Jun 02, 2010 12:20 pm


Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 7 guests