summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKarl Palsson <karlp@etactica.com>2020-06-30 10:38:44 +0000
committerPetr Štetiar <ynezz@true.cz>2020-07-11 11:15:12 +0200
commitf4e9bf73ac5c0ee6b8f240e2a2100e70ca56d705 (patch)
tree875a9e7f0f371e703d70f5412deb039655e12ced
parent53b9a2123fc6f0a7fa09ada066cbb0491c72bcf5 (diff)
downloadlibubox-f4e9bf73ac5c0ee6b8f240e2a2100e70ca56d705.tar.gz
examples/lua: attempt to highlight some traps
Ran into some issues with my fd event being garbage collected. As I never wanted to call :delete, I had seen no reason to keep the returned object, as my callback and upvalues were still valid. Signed-off-by: Karl Palsson <karlp@etactica.com>
-rwxr-xr-xexamples/uloop-example.lua8
1 files changed, 5 insertions, 3 deletions
diff --git a/examples/uloop-example.lua b/examples/uloop-example.lua
index 9b0684e..511b9ea 100755
--- a/examples/uloop-example.lua
+++ b/examples/uloop-example.lua
@@ -9,7 +9,7 @@ local udp = socket.udp()
udp:settimeout(0)
udp:setsockname('*', 8080)
--- timer example 1
+-- timer example 1 (will run repeatedly)
local timer
function t()
print("1000 ms timer run");
@@ -18,10 +18,10 @@ end
timer = uloop.timer(t)
timer:set(1000)
--- timer example 2
+-- timer example 2 (will run once)
uloop.timer(function() print("2000 ms timer run"); end, 2000)
--- timer example 3
+-- timer example 3 (will never run)
uloop.timer(function() print("3000 ms timer run"); end, 3000):cancel()
-- process
@@ -46,6 +46,8 @@ uloop.timer(
end, 2000
)
+-- Keep udp_ev reference, events will be gc'd, even if the callback is still referenced
+-- .delete will manually untrack.
udp_ev = uloop.fd_add(udp, function(ufd, events)
local words, msg_or_ip, port_or_nil = ufd:receivefrom()
print('Recv UDP packet from '..msg_or_ip..':'..port_or_nil..' : '..words)