From b81963a86deec5f4dc25e7d9f3883914095cf9dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Stehl=C3=ADk?= Date: Mon, 2 Apr 2018 08:35:34 +0200 Subject: [PATCH] net socket documentation clarification in FAQ (#2339) --- docs/en/lua-developer-faq.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/en/lua-developer-faq.md b/docs/en/lua-developer-faq.md index ab22db45..64f4c659 100644 --- a/docs/en/lua-developer-faq.md +++ b/docs/en/lua-developer-faq.md @@ -195,12 +195,15 @@ All Lua callbacks are called by C wrapper functions within the NodeMCU libraries * If you are running out of memory, then you might not be correctly clearing down Registry entries. One example is as above where you are setting up timers but not unregistering them. Another occurs in the following code fragment. The `on()` function passes the socket to the connection callback as it's first argument `sck`. This is local variable in the callback function, and it also references the same socket as the upvalue `srv`. So functionally `srv` and `sck` are interchangeable. So why pass it as an argument? Normally garbage collecting a socket will automatically unregister any of its callbacks, but if you use a socket as an upvalue in the callback, the socket is now referenced through the Register, and now it won't be GCed because it is referenced. Catch-22 and a programming error, not a bug. +Example of wrong upvalue usage in the callback: ```Lua srv:on("connection", function(sck, c) - svr:send(reply) + svr:send(reply) -- should be 'sck' instead of 'srv' end) ``` +Examples of correct callback implementations can be found in the [net socket documentation](modules/net.md#netsocketon). + * One way to check the registry is to use the construct `for k,v in pairs(debug.getregistry()) do print (k,v) end` to track the registry size. If this is growing then you've got a leak. ### How do I track globals