dnc40085 wrote:PROGRESS!!!!!1!!!!111!!!!11!1!!!1!!
I added stackdump() before and after each command that affects the Lua stack and learned how the stack is manipulated and where each function begins and ends. I figured out that the end of "wifi_scan_done" is the end of the line for the C function and I observed that when the function is done it does not remove the function stored in LUA_REGISTRYINDEX or set wifi_scan_succeed to "LUA_NOREF" so I added it. After this addition, the line "wifi.sta.getap(function() return end)" was no longer necessary and the memory use had dropped to 128 bytes.
This is what I added at the very end of wifi_scan_done:Code: Select allif(wifi_scan_succeed != LUA_NOREF)
{
luaL_unref(gL, LUA_REGISTRYINDEX, wifi_scan_succeed);
wifi_scan_succeed = LUA_NOREF;
}
This is the result:Code: Select all=node.heap()
22072
> wifi.sta.getap(function(T) for k,v in pairs(T) do print(k..":"..v) end end)
>
> myssid: 1a:fe:34:xx:xx:xx,-1
xfinitywifi: 96:1a:ca:xx:xx:xx,-91
Linksys_dd-wrt: 00:1d:7e:xx:xx:xx,-64
=node.heap()
21944
>
Now I just have to figure out how to get the last 128 bytes back...
One important think that must be checked when registering lua based callbacks into some other framework
like here the SDK provided framework.
There will typically some handler written in c being used in between.
In that case you have to ask yourself some questions:
Must the callback be unregistered?
Can that be safely done?
Or may it happen that the callback may be called when the lua function is already being freed?
What can happen then?
You have to coordinate the life cycles of the registrations and the lua objects making up the lua part of the callback.
In our case this is no problem but there is an open issue in the "net" module where callbacks happen after
callback structure is freed and using the garbage inside some freed objects.
Have fun!
Cal