From 079566cad9e7a089316e61d41a3f214bd2779e4c Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Sun, 20 Apr 2025 20:19:01 +0300 Subject: [PATCH] Fix memory leak in lua selection iterate Iterating over a large set of locations in a selection caused a memory leak. Lua couldn't do garbage collection in the middle of the iterator function, so it eventually ran out of space, and just quietly dropped stuff. --- src/nhlsel.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nhlsel.c b/src/nhlsel.c index f700a9ea3..306820cf9 100644 --- a/src/nhlsel.c +++ b/src/nhlsel.c @@ -925,7 +925,7 @@ l_selection_iterate(lua_State *L) if (argc == 2 && lua_type(L, 2) == LUA_TFUNCTION) { sel = l_selection_check(L, 1); selection_getbounds(sel, &rect); - for (y = rect.ly; y <= rect.hy; y++) + for (y = rect.ly; y <= rect.hy; y++) { for (x = max(1,rect.lx); x <= rect.hx; x++) if (selection_getpoint(x, y, sel)) { coordxy tmpx = x, tmpy = y; @@ -939,6 +939,8 @@ l_selection_iterate(lua_State *L) goto out; } } + lua_gc(L, LUA_GCCOLLECT, 0); + } } else { nhl_error(L, "wrong parameters"); /*NOTREACHED*/