From b92387afd49ecd453856bb407c7363ec8413c4ca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois-Simon=20Fauteux-Chapleau?=
 <francois-simon.fauteux-chapleau@savoirfairelinux.com>
Date: Wed, 11 Dec 2024 15:54:28 -0500
Subject: [PATCH] turn_cache: fix memory leak

GitLab: #632
Change-Id: I54a9e41f1cd026183a0fa3aa0891e49e99785517
---
 src/turn/turn_cache.cpp | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/turn/turn_cache.cpp b/src/turn/turn_cache.cpp
index a390df1..f07274d 100644
--- a/src/turn/turn_cache.cpp
+++ b/src/turn/turn_cache.cpp
@@ -191,7 +191,11 @@ TurnCache::testTurn(IpAddr server)
                 std::lock_guard lock(shutdownMtx_);
                 if (onConnectedTimer_) {
                     onConnectedTimer_->expires_at(std::chrono::steady_clock::now());
-                    onConnectedTimer_->async_wait(std::bind(&TurnCache::onConnected, shared_from_this(), std::placeholders::_1, ok, server));
+                    onConnectedTimer_->async_wait([w=weak_from_this(), ok, server](const asio::error_code& ec) {
+                        if (auto shared = w.lock()) {
+                            shared->onConnected(ec, ok, server);
+                        }
+                    });
                 }
             });
     } catch (const std::exception& e) {
@@ -229,7 +233,11 @@ TurnCache::refreshTurnDelay(bool scheduleNext)
         if(logger_) logger_->warn("[Account {:s}] Cache for TURN resolution failed.", accountId_);
         if (refreshTimer_) {
             refreshTimer_->expires_at(std::chrono::steady_clock::now() + turnRefreshDelay_);
-            refreshTimer_->async_wait(std::bind(&TurnCache::refresh, shared_from_this(), std::placeholders::_1));
+            refreshTimer_->async_wait([w=weak_from_this()](const asio::error_code& ec) {
+                if (auto shared = w.lock()) {
+                    shared->refresh(ec);
+                }
+            });
         }
         if (turnRefreshDelay_ < std::chrono::minutes(30))
             turnRefreshDelay_ *= 2;
-- 
GitLab