Since using a destructor doesn't seem to work all that well, try using atexit instead.