Repository: neovim
Issue: `ml_get` error and memory leak when unloading buffer if `on_detach` callback calls `bufload` on it
Labels: bug,api,has:repro

### Problem

I want to use `vim.api.nvim_win_close` to close window in other tabpage.
It closed window but print many error messages `Vim:E315: ml_get: invalid lnum: 1` from treesitter callback.

```
E5108: Error executing lua [string ":lua"]:1: Vim:Error executing lua callback: ....4/share/nvim/runtime/lua/vim/treesitter/highlighter
.lua:148: Vim:Error executing lua callback: Vim:E315: ml_get: invalid lnum: 1
stack traceback:
        [C]: in function 'bufload'
        vim/_editor.lua: in function 'region'
        ...ar/neovim/0.9.4/share/nvim/runtime/lua/vim/highlight.lua:35: in function 'range'
        ...re/nvim/plugins/nvim-ts-rainbow/lua/rainbow/internal.lua:82: in function 'update_range'
        ...re/nvim/plugins/nvim-ts-rainbow/lua/rainbow/internal.lua:186: in function 'cb'
        ...4/share/nvim/runtime/lua/vim/treesitter/languagetree.lua:746: in function '_do_callback'
        ...4/share/nvim/runtime/lua/vim/treesitter/languagetree.lua:180: in function 'invalidate'
        ...4/share/nvim/runtime/lua/vim/treesitter/languagetree.lua:898: in function '_on_detach'
        ...r/neovim/0.9.4/share/nvim/runtime/lua/vim/treesitter.lua:73: in function <...r/neovim/0.9.4/share/nvim/runtime/lua/vim/trees
itter.lua:69>
        [C]: in function 'nvim_win_close'
        [string ":lua"]:1: in main chunk
stack traceback:
        [C]: in function '__newindex'
        ....4/share/nvim/runtime/lua/vim/treesitter/highlighter.lua:148: in function 'destroy'
        ....4/share/nvim/runtime/lua/vim/treesitter/highlighter.lua:184: in function 'on_detach'
        ....4/share/nvim/runtime/lua/vim/treesitter/highlighter.lua:83: in function 'cb'
        ...4/share/nvim/runtime/lua/vim/treesitter/languagetree.lua:746: in function '_do_callback'
        ...4/share/nvim/runtime/lua/vim/treesitter/languagetree.lua:899: in function '_on_detach'
        ...r/neovim/0.9.4/share/nvim/runtime/lua/vim/treesitter.lua:73: in function <...r/neovim/0.9.4/share/nvim/runtime/lua/vim/trees
itter.lua:69>
        [C]: in function 'nvim_win_close'
        [string ":lua"]:1: in main chunk
stack traceback:
        [C]: in function 'nvim_win_close'
        [string ":lua"]:1: in main chunk
```

### Steps to reproduce

1. Use nvim to open two files whose content highlighted by treesitter on two tabpages. Each tabpage contains one window.
2. `:lua print(vim.api.nvim_get_current_win())` get current window number. For example, it prints `1000`.
3. Switch to another tabpage.
4. `:lua vim.api.nvim_win_close(1000, false)` to close the previous window. It closed window but print many error messages.

Note:
1. Empty file content will not print error.
2. Close current window in same tabpage will not print error.
3. Using `vim.api.nvim_buf_delete` has same issue with `nvim_win_close`.

### Expected behavior

Just close the window without any error.

### Neovim version (nvim -v)

NVIM v0.9.4

### Vim (not Nvim) behaves the same?

not sure

### Operating system/version

macOS 14.1.2 (23B2091)

### Terminal name/version

iTerm2 3.5.0beta17

### $TERM environment variable

xterm-256color

### Installation

homebrew