'"memory allocation error: block too big" doing simple integer division
I have a weird issue. LUA 5.3.5, compiled on STM32F429. Free RAM is about 1Mb (memory allocation is using external SDRAM, not the more limited internal RAM on the STM32). Note that working with things like strings works fine, as well. It only seems to be division causing the problem.
This script works:
a=100
b=20
c=a+b
print(c)
This script returns "memory allocation error: block too big:"
a=100
b=20
c=a/b
print(c)
Further research is showing that the problem is not with the division, at all. It is with tostring() which is called by print(). For some reason, tostring() is trying to allocate too much memory when dealing with the result from direct division.
In lstring.c, is the following:
luaS_newlstr():
if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char))
luaM_toobig(L);
When the issue occurs, l == 0xd0600f56 (interestingly, that is a memory address location in the range of the external SDRAM, rather than a valid string size).
If I modify the LUA script to do the following, it works fine:
a=100
b=20
c=math.floor(a/b)
print(c)
I checked and in both cases, c is type==number
As for the question regarding the memory allocation, we are using the dlmalloc() library, configured like this during LUA startup:
ezCmdLua = lua_newstate(ezlua_poolalloc, NULL);
int error = luaL_loadbuffer(ezCmdLua, bfr, len, "ezCmdLua");
if (!error)
{
error = lua_pcall(ezCmdLua, 0, 0, 0);
if (error) {
...
}
}
....
static void *ezlua_poolalloc (void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud; (void)osize; /* not used */
if (nsize == 0) {
dlfree(ptr);
return NULL;
}
else
return dlrealloc(ptr, nsize);
}
I have confirmed that memory allocation is working properly, and I can do things like string manipulation and printing of strings with no problem at all. In fact, when debugging this issue, the luaS_newlstr() function is called several times prior to the issue occurring, and each time l (the length of the string) is a reasonable value. That is, until I try to print the result of the division. Moving the division around in the script makes no difference (ie, adding things before it like other print statements), so I doubt the stack is being trashed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|