From 4e73b05ff0eeaf2cefaf5757dc1c2f523da93191 Mon Sep 17 00:00:00 2001 From: olivierkeke Date: Wed, 13 Jan 2021 20:40:25 +0100 Subject: [PATCH] [teleinfo] Fix memory leak (#9724) * Use set instead list to avoid duplicate listeners * Remove from listeners when bridge is not ONLINE * Unsubscribe from bridge handler events when thing handler is disposed Signed-off-by: Olivier Marceau --- .../TeleinfoAbstractControllerHandler.java | 6 ++-- ...leinfoAbstractElectricityMeterHandler.java | 29 ++++++++++++++----- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/bundles/org.openhab.binding.teleinfo/src/main/java/org/openhab/binding/teleinfo/internal/handler/TeleinfoAbstractControllerHandler.java b/bundles/org.openhab.binding.teleinfo/src/main/java/org/openhab/binding/teleinfo/internal/handler/TeleinfoAbstractControllerHandler.java index 640baec39..4397f4d05 100644 --- a/bundles/org.openhab.binding.teleinfo/src/main/java/org/openhab/binding/teleinfo/internal/handler/TeleinfoAbstractControllerHandler.java +++ b/bundles/org.openhab.binding.teleinfo/src/main/java/org/openhab/binding/teleinfo/internal/handler/TeleinfoAbstractControllerHandler.java @@ -12,10 +12,10 @@ */ package org.openhab.binding.teleinfo.internal.handler; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.List; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.binding.teleinfo.internal.TeleinfoDiscoveryService; @@ -32,7 +32,7 @@ import org.openhab.core.thing.binding.ThingHandlerService; @NonNullByDefault public abstract class TeleinfoAbstractControllerHandler extends BaseBridgeHandler { - private List listeners = Collections.synchronizedList(new ArrayList<>()); + private Set listeners = new CopyOnWriteArraySet<>(); public TeleinfoAbstractControllerHandler(Bridge bridge) { super(bridge); diff --git a/bundles/org.openhab.binding.teleinfo/src/main/java/org/openhab/binding/teleinfo/internal/handler/TeleinfoAbstractElectricityMeterHandler.java b/bundles/org.openhab.binding.teleinfo/src/main/java/org/openhab/binding/teleinfo/internal/handler/TeleinfoAbstractElectricityMeterHandler.java index 35d2cee7c..ffa711c3f 100644 --- a/bundles/org.openhab.binding.teleinfo/src/main/java/org/openhab/binding/teleinfo/internal/handler/TeleinfoAbstractElectricityMeterHandler.java +++ b/bundles/org.openhab.binding.teleinfo/src/main/java/org/openhab/binding/teleinfo/internal/handler/TeleinfoAbstractElectricityMeterHandler.java @@ -65,22 +65,35 @@ public abstract class TeleinfoAbstractElectricityMeterHandler extends BaseThingH @Override public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) { + TeleinfoAbstractControllerHandler controllerHandler = getControllerHandler(); if (bridgeStatusInfo.getStatus() != ThingStatus.ONLINE) { + if (controllerHandler != null) { + controllerHandler.removeListener(this); + } updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, ERROR_OFFLINE_CONTROLLER_OFFLINE); return; } - Bridge bridge = getBridge(); - if (bridge != null) { - TeleinfoAbstractControllerHandler controllerHandler = (TeleinfoAbstractControllerHandler) bridge - .getHandler(); - if (controllerHandler != null) { - controllerHandler.addListener(this); - updateStatus(ThingStatus.ONLINE); - } + if (controllerHandler != null) { + controllerHandler.addListener(this); + updateStatus(ThingStatus.ONLINE); } } + @Override + public void dispose() { + TeleinfoAbstractControllerHandler controllerHandler = getControllerHandler(); + if (controllerHandler != null) { + controllerHandler.removeListener(this); + } + super.dispose(); + } + + private @Nullable TeleinfoAbstractControllerHandler getControllerHandler() { + Bridge bridge = getBridge(); + return bridge != null ? (TeleinfoAbstractControllerHandler) bridge.getHandler() : null; + } + @Override public void handleCommand(ChannelUID channelUID, Command command) { // no commands supported