06-29-2009 09:23 PM
Is there a way to detect when an application exits. I come from a .Net background and have used "destructors" to detect when an application exits. Maybe I missed something but it doesn't seem like there are destructors in java so I can't detect when the app exits.
I understand you can just override the onClose() function but I am writing a library that creates temp files internally and want to delete the files on when the app exits.
Is there any way to do this?
06-29-2009 09:52 PM
06-29-2009 10:10 PM
So wait. Why isn't onClose useful for this?
He's developing a library (I'm going to take a wild guess here and say there are no Screens in the library). With Screen.onClose, the responsibility to make the call that frees the temp files is shifted from the library developer to the app developer. If the app developer doesn't read the docs, the call isn't made and files are left stranded.
The design would be much nicer if the library itself could take care of that. But on BlackBerry, that is not possible. Destroctors in JAVA never really worked. Most BlackBerry apps terminate with System.exit() which is a very abrupt exit.
You will need to ask the app developer to call lib.cleanup() before calling System.exit().
One thing you can do from the lib, is cleanup when starting (assuming you can tell your own temp files from non-related temp files). That way, if the app developer neglects to make the call, at least the lib isn't building a pile of garbage on the drive.
06-29-2009 10:27 PM
Perfect, you took what I for whatever reason couldn't put into words and explained it.
As for your idea, I like the idea but worry that some files could be very large (I have an idea (not the current one) for a on device Video Editor and working with videos is a very, very, very, ... memory and hdd intensive so I wouldn't want to leave these temp files on a device) and could be required to be removed when the app is closed.
06-29-2009 11:24 PM
Leaving very large files around is actually better in this regard than leaving small files, because they are hard to miss. After a couple of runs in the simulator the app de will surely notice the space running out =)
Provide a static cleanup method, document it, and hand off responsibility to the app dev. Not the prettiest of designs, but it is the best you can do on the BlackBerry.
06-29-2009 11:55 PM
Another strategy might be to have the calling application pass a reference to itself to the library on initialization.
The library might then create a watchdog thread that periodically checks Application.isAlive(), closing up shop and releasing resources when the last client is dead.
06-30-2009 07:16 AM
I thought about it last night and I think I'm going to do what bneeman said and have the app dev take care of exiting... for now. I won't say the problem is solved because I want this to be automated. What RexDoug said seems like a good idea I just hope that a thread with a loop to check the app does not get shut down by the VM before everything else. But for now, just so that I can finish this app I am working on I will call an temp Exit function.
Thanks for the ideas and help, will post anything if I figure out how to automate it.
06-01-2010 06:45 AM - edited 06-01-2010 08:39 AM
Would the following variation of RexDoug's aproach work?
- Start the watchdog thread in the library's libmain() method.
- it maintiains a Hashtable of resources keyed by Application instances where each time a given lib service is requested the requestor Application gets added if not aready there .
- when some key.isAlive=false, do cleanup
I'm assuming said Hashtable would be a singleton in the RuntimeStore.
I'm also asuming Hashtable is threadsafe as well.
Admitedly this aproach (if doable at all) would incur in some nasty overhead but, come on, OP was talking about video processing!
03-30-2011 05:24 AM
I thought about overloading finalize function to do that Exit handle but i fear this happend too late ...
Are there some samples that "release a library" on exit ?