From 651fa4a3b6988b3a37c0de5793f8d96fda0a09d5 Mon Sep 17 00:00:00 2001 From: David Capello Date: Tue, 19 Oct 2021 10:57:25 -0300 Subject: [PATCH] Stop the auxiliar ui::Timer when the last WebSocket is GC'd --- src/app/script/websocket_class.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/app/script/websocket_class.cpp b/src/app/script/websocket_class.cpp index cdb2c3658..f5d85189c 100644 --- a/src/app/script/websocket_class.cpp +++ b/src/app/script/websocket_class.cpp @@ -33,6 +33,15 @@ namespace { static std::unique_ptr g_timer; static std::set g_connections; +static void close_ws(ix::WebSocket* ws) +{ + ws->stop(); + + g_connections.erase(ws); + if (g_connections.empty()) + g_timer.reset(); +} + int WebSocket_new(lua_State* L) { static std::once_flag f; @@ -108,7 +117,7 @@ int WebSocket_new(lua_State* L) int WebSocket_gc(lua_State* L) { auto ws = get_ptr(L, 1); - ws->stop(); + close_ws(ws); delete ws; return 0; } @@ -182,13 +191,7 @@ int WebSocket_connect(lua_State* L) int WebSocket_close(lua_State* L) { auto ws = get_ptr(L, 1); - ws->stop(); - - g_connections.erase(ws); - if (g_connections.empty()) { - g_timer = nullptr; - } - + close_ws(ws); return 0; }