destroyApp() not called by at^sjam=2 | Telit Cinterion IoT Developer Community
September 26, 2016 - 9:12am, 3756 views
In our application we're using 3 instances of an ATCommand, which appears to be the maximum amount. Our application is an end-user device, in which it is not allowed for an end-user to turn the device off or reboot it (this is under software control).
When we autonomously update the application, we need to stop the midlet, install the new one and start it.
It now appears that the at^sjam=2 command doesn't call the destroyApp() method, causing the ATCommand instances not to be released and hence starting the new midlet fails because the ATCommand instances are exhausted.
Why is the destroyApp not called, this seems very logical to release resources, there is currently no other way in doing this or is there another method to release the resources?
Hello,
The destroyApp() method will only be called by the application management system if the startApp() method is terminated. So you should not stay in the startApp() method for a whole time but initiate the application, start the necessary threads and leave.
Please refer to the Java User's Guide, chapter 6.2 MIDlet Life Cycle.
Best regards,
Bartłomiej
Aha, that wasn't clear for me. That would simplify things and make life easy. I'll give it a go.
Yes, that appears to solve this issue, thanks.
Hello Mr. Bartlomiej,
The startApp() method is the main thread (the thread running for the whole life time of the App). So, if we create threads within startApp() and leave ... would it be fine to create new threads within other threads like normally done? Like for example I create seperate thread for event listening from another thread whic was (created from StartApp()). I believe it is not necessary to always create and start a thread within startApp().
Best Regards,
Umair
Hello,
startApp() method is intended to initiate and start the application. It should be left afterwards and not be treated as the main thread and not be kept running for the whole life of the application (it is practiced for the short demonstrators but should not be for the final versions).
destroyApp() method is called automatically before the application is stopped (for example by AT command or OTAP). But for this to be possible startApp() method must be executed and left. If not it will not work like that. destroyApp() can also be called explicitly. It should save any state and release all resources. notifyDestroyed() should be called in the end to notify the application management software that all the cleaning is done.
Regards,
Bartłomiej
Hello Mr. Bartlomiej,
startApp() method is intended to initiate and start the application. It should be left afterwards and not be treated as the main thread and not be kept running for the whole life of the application ( I thought that startApp() method is just like a infinite main thread which keeps on running ). How does the application runs if startApp() method is terminated ( i.e only initiate the application and run few threads). Is there any other thread where the whole application (MIDlet) runs in the background ? I mean if startApp() is terminated then how would the MIDlet still runs ?
- Is there any document regarding this detail ?
Best Regards,
Umair
Hello,
There's no main method in MIDlet. There are three possible states in the MIDlet lifecycle: Paused, Active and Destroyed. After execution of startApp() method it enters the Active state and any code that you have initiated will be working, any thread that you have created will be running.
You can read more in the Java User's Guide document and IMP-NG documentation (http://download.oracle.com/otndocs/jcp/imp_ng-1.0-final-oth-JSpec/).
Regards,
Bartłomiej
Hello Mr. Bartlomiej,
Thanks for your reply. I have one query regarding destroyApp() method. As you said we can call it explicitly, I want to call it when a certain boolean flag (global boolean variable) is true. Here is my scenario:
In my Thread1 (created in startApp() method) , I am constantly checking whether one flag is true or not and when it become true I have to call the destroyApp() method. The flag will become true if some conditions being met in another Thread let's call it Thread 2. I can also check whether the flag is true or not in Thread2 and destroyApp() ( would it be a good solution) or calling destroyApp() from Thread1 when the flag is true is the better solution?
Best Regards,
Umair
Hello,
Yes you can call destroyApp() from your code. But still you should leave startApp() method after application initialization so that it could be called automatically when needed.
I don't think that it matters which thread calls destroyApp(). You should choose the solution which is easier or better from the point of view of your application's logic.
Regards,
Bartłomiej