[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 <hollysaiqs@marceau.ovh>
This commit is contained in:
olivierkeke 2021-01-13 20:40:25 +01:00 committed by GitHub
parent 3e7014dafb
commit 4e73b05ff0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 11 deletions

View File

@ -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<TeleinfoControllerHandlerListener> listeners = Collections.synchronizedList(new ArrayList<>());
private Set<TeleinfoControllerHandlerListener> listeners = new CopyOnWriteArraySet<>();
public TeleinfoAbstractControllerHandler(Bridge bridge) {
super(bridge);

View File

@ -65,20 +65,33 @@ 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);
}
}
@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