The Problem
Recently I came across a strange problem when deploying a Java J2ME application to a new Blackberry Phone (the 8900 "Javelin" Curve). The app simply captures data from the user, performs a little bit of processing and sends the resulting data to a server over http/https. The app installed and ran absolutely fine on several older BB devices (such as the 8320 Curve) and also the new 8900 simulator. However, when I installed the app on an actual physical 8900 device for final testing the app would hang at arbitary points when sending or receiving data over http. Tracking down the cause of the problem took a long time. The first step was looking at the Blackberry event log (by holding the alt-key and entering LGLG via the keypad), and it showed that a communication Exception was being thrown around the time of the hang.
The problem is that no Exception was being caught be our code (as we had try-catch blocks at appropriate places). Running the app through the Blackberry JDE with the phone debug-attached (via USB ) again confirmed that no Exception was being seen within the application, but we did learn that the frequency of Garbage Collection was increasing exponentially as the number of http communications were established and closed. At this point I assumed that either the http connections to the server were not getting closed properly, or there was a strange memory leak in the app somewhere, but why were we not seeing this problem in other BBL devices?
My Solution
After posting in the Blackberry J2ME developer forum (see link below) and playing around with the app's debug output we eventually tracked the main cause of the problem down to http connections not being closed, but for a strange reason... A lot of code within the http communication class was written as follows:
try {
HttpConnection hc = null;
try {
hc = (HttpConnection) conn.open("<<url>>");
} catch (IOException iox) {
//do stuff, but although execution to continue in this method
}hc.close();
}catch (Exception e) { //do stuff }
Changing it to close the connection in a finally statement solved the issue:
try {
HttpConnection hc = null;
try {
hc = (HttpConnection) conn.open("<<url>>");
} catch (IOException iox) {
//do stuff, but although execution to continue in this method
} finally {
hc.close();}catch (Exception e) { //do stuff }
I'm not exactly sure why, although not including the call to the close() method in a finally block (as demonstrated by the first code fragment) could be considered bad form, the connection should have been closed after any IOException was caught? Regardless changing all of the connections to close in the finally method eliminated the problem we were seeing...
If this didn't help, here any some other causes
Upon reading various forums I also learned that a J2ME app hanging on a BB can also be symptomatic of the following causes (which may be worth further research if my solution didn't help)
- UI Dialog boxes being hidden (behind the current app) which require user input
- Garbage collection due to memory leaks etc. (full GC can take up to 30 seconds to complete)
- Deadlock between competing threads
- Incorrect threading in general i.e. attempting to open socket connections using the main thread (a new thread should be spawned)
Useful links
My BB Forum Post (thanks to John for replying) : http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=51749#M51749
Daniel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Daniel Bryant (Director) | Tai-Dev Ltd www.tai-dev.co.uk - IT Consultancy Services Specialising in JEE, Web 2.0 and RDBMS

