Even if your code is power-aware, you are not guaranteed to get notified about a loss of power.
A better way to handle these sorts of situations is to do your cleanup during package activation. Package activation happens every time the power is turned on or your package in newly installed.
There are two operations which are called on every package activation: Load() and Install().
Load() is called for every instance in your instance definition file of classes which inherit from HasLoad. You should call InheritedLoad() and then do whatever per-instance initialization or cleanup is needed.
Install() is called for every class which overrides Install(). You should do whatever per-class initialization or cleanup is needed. You should not call InheritedInstall().
I want to create transient objects when my package is unpacked. I've got code to do this work in Install() and Load(). These seem to work when the user is powering up the communicator, but they are not called after my package is packed then unpacked. Why aren't these methods being called at this time?
Install() and Load() are called when the transient clusters are created. In currently available Magic Cap communicators, transient memory goes away when the communicator loses power, so when it's turned on again, these clusters must be recreated. However, when a package is packed up, its transient cluster remains, but ObjectIDs stored in package persistent objects that reference objects in transient memory are set to nil. Pointers to buffers in transient memory are unaffected. If you're creating buffers, you won't need to reallocate them when your package is unpacked, but if you create other types of objects in transient memory, and refer to them from persistent objects, you'll need to refresh these references in an InstallInto() override.
When the communicator is turned on, I find that methods like Load() and InstallInto() are not being called again on my objects, even though the entire transient cluster was destroyed when the communicator powered off. Where can I recreate my transient objects?
It would be much simpler if I could just get notified when my package is about to go away so I can clean up before the fact. Is there a way to do this? InstallInto() does not get called on power up, but Install() and Load() do. However, Load() only gets called on objects that were created in your Objects.Def file, not any objects that are subsequently allocated.
Magic Cap doesn't notify your package your package is about to go away, because there are many instances when it can go away without warning, such as when the power is lost, the user force powers off, or your RAM card is ejected. During these times, your package doesn't have any opportunity to run any code before it goes away, so you need to design your package so that it doesn't need to. Load() is called for each source object, and Install() is called for each class. They are ideal for allocating transient objects after power on.