terminate_connection(): Avoid use-after-free and double-free for