From ef5df1ae52e58a9d6b6487fe16af5f3fe43fff3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sami=20Kalliom=C3=A4ki?= Date: Thu, 26 Oct 2017 15:17:48 +0200 Subject: [PATCH] Fix WebSocketObserver getting garbage collected. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apparently WebSocketObserver gets garbage collected if it is not stored by us. This caused some external tests to break. Bug: None Change-Id: If62786e84f84a5a63172d67962bb4de8ae3e8479 Reviewed-on: https://webrtc-review.googlesource.com/16100 Reviewed-by: Magnus Jedvert Commit-Queue: Sami Kalliomäki Cr-Commit-Position: refs/heads/master@{#20449} --- .../appspot/apprtc/WebSocketChannelClient.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/examples/androidapp/src/org/appspot/apprtc/WebSocketChannelClient.java b/examples/androidapp/src/org/appspot/apprtc/WebSocketChannelClient.java index bf241fe8cb..c013c76f0d 100644 --- a/examples/androidapp/src/org/appspot/apprtc/WebSocketChannelClient.java +++ b/examples/androidapp/src/org/appspot/apprtc/WebSocketChannelClient.java @@ -10,22 +10,18 @@ package org.appspot.apprtc; -import org.appspot.apprtc.util.AsyncHttpURLConnection; -import org.appspot.apprtc.util.AsyncHttpURLConnection.AsyncHttpEvents; - import android.os.Handler; import android.util.Log; - import de.tavendo.autobahn.WebSocket.WebSocketConnectionObserver; import de.tavendo.autobahn.WebSocketConnection; import de.tavendo.autobahn.WebSocketException; - -import org.json.JSONException; -import org.json.JSONObject; - import java.net.URI; import java.net.URISyntaxException; import java.util.LinkedList; +import org.appspot.apprtc.util.AsyncHttpURLConnection; +import org.appspot.apprtc.util.AsyncHttpURLConnection.AsyncHttpEvents; +import org.json.JSONException; +import org.json.JSONObject; /** * WebSocket client implementation. @@ -34,7 +30,6 @@ import java.util.LinkedList; * passed in a constructor, otherwise exception will be thrown. * All events are dispatched on the same thread. */ - public class WebSocketChannelClient { private static final String TAG = "WSChannelRTCClient"; private static final int CLOSE_TIMEOUT = 1000; @@ -46,6 +41,9 @@ public class WebSocketChannelClient { private String roomID; private String clientID; private WebSocketConnectionState state; + // Do not remove this member variable. If this is removed, the observer gets garbage collected and + // this causes test breakages. + private WebSocketObserver wsObserver; private final Object closeEventLock = new Object(); private boolean closeEvent; // WebSocket send queue. Messages are added to the queue when WebSocket @@ -92,7 +90,7 @@ public class WebSocketChannelClient { Log.d(TAG, "Connecting WebSocket to: " + wsUrl + ". Post URL: " + postUrl); ws = new WebSocketConnection(); - WebSocketObserver wsObserver = new WebSocketObserver(); + wsObserver = new WebSocketObserver(); try { ws.connect(new URI(wsServerUrl), wsObserver); } catch (URISyntaxException e) {