mirror of
https://github.com/neovim/neovim.git
synced 2025-09-07 20:08:17 +00:00
fix(api): nvim_create_buf leaks memory if buffer is loaded early
Problem: memory leak in nvim_create_buf if buflist_new autocommands load the new buffer early. Solution: do not open a memfile in that case.
This commit is contained in:
@@ -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
|
// 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.
|
// a later nvim_buf_set_lines call would have needed to "open" the buffer.
|
||||||
try_start();
|
if (buf->b_ml.ml_mfp == NULL) {
|
||||||
block_autocmds();
|
try_start();
|
||||||
int status = ml_open(buf);
|
block_autocmds();
|
||||||
unblock_autocmds();
|
int status = ml_open(buf);
|
||||||
try_end(err);
|
unblock_autocmds();
|
||||||
if (status == FAIL) {
|
try_end(err);
|
||||||
goto fail;
|
if (status == FAIL) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set last_changedtick to avoid triggering a TextChanged autocommand right
|
// Set last_changedtick to avoid triggering a TextChanged autocommand right
|
||||||
|
@@ -3135,6 +3135,15 @@ describe('API', function()
|
|||||||
-- nowadays this works because we don't execute any spurious autocmds at all #24824
|
-- nowadays this works because we don't execute any spurious autocmds at all #24824
|
||||||
assert_alive()
|
assert_alive()
|
||||||
end)
|
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)
|
end)
|
||||||
|
|
||||||
describe('nvim_get_runtime_file', function()
|
describe('nvim_get_runtime_file', function()
|
||||||
|
Reference in New Issue
Block a user