I need to write on a device sth like "bye bye" before my program exits. (User closes the window).

How can I do that?
There must be a signal which is sent before destroying all the instances of the app.
I must be sure that no instances of my objects are destroyed, so I can write on the device.

After writing my "bye bye" message on the device, the app can be closed (every instances of my objects can be destroyed).