<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, 29 Oct 2024 at 07:49, user202729--- via luatex <<a href="mailto:luatex@tug.org">luatex@tug.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I notice that repeated calls to `token.set_macro` consumes up memory, even though the macro themselves are freed.<br>
<br>
For example: run the following command<br>
<br>
```<br>
lualatex '\directlua{for i=1,50000000 do token.set_macro("a", "") end}\stop' </dev/null<br>
```<br>
<br>
This will consume a few GB of memory before terminating.<br>
<br>
After looking at the source code, the reason is because the reference count is not correctly zeroed, which leads to the token list not being freed when the macro is overwritten.<br>
<br>
The following patch should fix the issue.<br>
<br>
<br>
```<br>
diff --git a/source/texk/web2c/luatexdir/lua/lnewtokenlib.c b/source/texk/web2c/luatexdir/lua/lnewtokenlib.c<br>
index 7c81e46..1b163b7 100644<br>
--- a/source/texk/web2c/luatexdir/lua/lnewtokenlib.c<br>
+++ b/source/texk/web2c/luatexdir/lua/lnewtokenlib.c<br>
@@ -1265,9 +1265,9 @@ static int set_macro(lua_State * L)<br>
const char *se = str + lstr;<br>
p = temp_token_head;<br>
set_token_link(p, null);<br>
- /* this left brace is used to store the number of arguments */<br>
- fast_store_new_token(left_brace_token);<br>
- /* and this ends the not present arguments, and no: we will not support arguments here*/<br>
+ /* reference count */<br>
+ fast_store_new_token(0);<br>
+ /* this ends the not present arguments, and no: we will not support arguments here*/<br>
fast_store_new_token(end_match_token);<br>
while (str < se) {<br>
/* hh: str2uni could return len too (also elsewhere) */<br>
@@ -1334,7 +1334,7 @@ static int set_macro(lua_State * L)<br>
halfword q; /* new node being added to the token list via |store_new_token| */<br>
p = temp_token_head;<br>
set_token_info(p,null);<br>
- fast_store_new_token(left_brace_token);<br>
+ fast_store_new_token(0);<br>
fast_store_new_token(end_match_token);<br>
define(cs, call_cmd + (a % 4), token_link(temp_token_head));<br>
}<br>
```<br>
<br>
Please take a look. Thank you.<br>
<br></blockquote><div><br></div><div>commit cd4888ec08e37cf0beae04be0a904d1df17eac41 <br>Author: Luigi Scarso <<a href="mailto:luigi.scarso@gmail.com">luigi.scarso@gmail.com</a>><br>Date: Sun Nov 3 13:17:40 2024 +0100<br><br> Fixed a memory leak in token.put_next() and token.set_macro() (thanks to <a href="mailto:user202729@protonmail.com">user202729@protonmail.com</a>)<br></div><div><br></div><div>(also in texlive)</div><div><br></div><div>--</div><div>luigi</div></div></div>