aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/api/vim.c16
-rw-r--r--test/functional/api/vim_spec.lua9
2 files changed, 18 insertions, 7 deletions
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c
index 24ad7d5fbc..7a9094557d 100644
--- a/src/nvim/api/vim.c
+++ b/src/nvim/api/vim.c
@@ -967,13 +967,15 @@ Buffer nvim_create_buf(Boolean listed, Boolean scratch, Error *err)
// Open the memline for the buffer. This will avoid spurious autocmds when
// a later nvim_buf_set_lines call would have needed to "open" the buffer.
- try_start();
- block_autocmds();
- int status = ml_open(buf);
- unblock_autocmds();
- try_end(err);
- if (status == FAIL) {
- goto fail;
+ if (buf->b_ml.ml_mfp == NULL) {
+ try_start();
+ block_autocmds();
+ int status = ml_open(buf);
+ unblock_autocmds();
+ try_end(err);
+ if (status == FAIL) {
+ goto fail;
+ }
}
// Set last_changedtick to avoid triggering a TextChanged autocommand right
diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua
index 09a45242ec..a3f70efcea 100644
--- a/test/functional/api/vim_spec.lua
+++ b/test/functional/api/vim_spec.lua
@@ -3135,6 +3135,15 @@ describe('API', function()
-- nowadays this works because we don't execute any spurious autocmds at all #24824
assert_alive()
end)
+
+ it('no memory leak when autocommands load the buffer immediately', function()
+ exec([[
+ autocmd BufNew * ++once call bufload(expand("<abuf>")->str2nr())
+ \| let loaded = bufloaded(expand("<abuf>")->str2nr())
+ ]])
+ api.nvim_create_buf(false, true)
+ eq(1, eval('g:loaded'))
+ end)
end)
describe('nvim_get_runtime_file', function()