Problem: heap-use-after-free possible when entering Terminal mode if autocommands close the terminal. Solution: set the refcount. Skip to the end if we must close the terminal.