From 5b6dd8e3f77a1e2deec0b4be32ca34759a7027cf Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 26 May 2022 15:18:21 +0200 Subject: [PATCH 1/2] [disco] Fix memory leak in ServiceDiscoveryManager The lambda we schedule in 25ms captures a strong reference to the XMPPConnection. However the lambda is part of the scheduled action, which we save in the renewEntityCapsScheduledAction field. This causes a memory leak, since the ServiceDiscoveryManager now holds a strong reference to its XMPPConnection. Fix this by obtaining the strong reference to the XMPPConnection, if one still exists, within the lambda. Fixes SMACK-926. Reported-by: Damian Minkov --- .../jivesoftware/smackx/disco/ServiceDiscoveryManager.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java index 312b559b5..fa60edc92 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java @@ -941,9 +941,12 @@ public final class ServiceDiscoveryManager extends Manager { } } - final XMPPConnection connection = connection(); - renewEntityCapsScheduledAction = scheduleBlocking(() -> { + final XMPPConnection connection = connection(); + if (connection == null) { + return; + } + renewEntityCapsPerformed.incrementAndGet(); DiscoverInfoBuilder discoverInfoBuilder = DiscoverInfo.builder("synthetized-disco-info-response") From 69e81b748ed95a1b4422c44141a39968691475d9 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 26 May 2022 15:41:40 +0200 Subject: [PATCH 2/2] [disco] Only perform entity capabilities work if there are any listeners --- .../jivesoftware/smackx/disco/ServiceDiscoveryManager.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java index fa60edc92..9fb69ee3c 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java @@ -1,6 +1,6 @@ /** * - * Copyright 2003-2007 Jive Software, 2018-2020 Florian Schmaus. + * Copyright 2003-2007 Jive Software, 2018-2022 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -933,6 +933,10 @@ public final class ServiceDiscoveryManager extends Manager { * Notify the {@link EntityCapabilitiesChangedListener} about changed capabilities. */ private synchronized void renewEntityCapsVersion() { + if (entityCapabilitiesChangedListeners.isEmpty()) { + return; + } + renewEntityCapsRequested++; if (renewEntityCapsScheduledAction != null) { boolean canceled = renewEntityCapsScheduledAction.cancel();