fix possible crash at exit