Commit b81963a8 authored by Petr Stehlík's avatar Petr Stehlík Committed by Marcel Stör
Browse files

net socket documentation clarification in FAQ (#2339)

parent 9af4f584
...@@ -195,12 +195,15 @@ All Lua callbacks are called by C wrapper functions within the NodeMCU libraries ...@@ -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. * 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 ```Lua
srv:on("connection", function(sck, c) srv:on("connection", function(sck, c)
svr:send(reply) svr:send(reply) -- should be 'sck' instead of 'srv'
end) 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. * 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 ### How do I track globals
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment