Throw IllegalStateException if native objects are used after dispose.

This makes it easier to debug issues related to double dispose /
use after dispose.

Bug: webrtc:7566, webrtc:8297
Change-Id: I07429b2b794deabb62b5f3ea1cf92eea6f66a149
Reviewed-on: https://webrtc-review.googlesource.com/102540
Commit-Queue: Sami Kalliomäki <sakal@webrtc.org>
Reviewed-by: Paulina Hensman <phensman@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24894}
This commit is contained in:
Sami Kalliomäki
2018-09-28 14:38:21 +02:00
committed by Commit Bot
parent dca5a2ca73
commit ee05e90297
15 changed files with 213 additions and 35 deletions

View File

@ -102,7 +102,7 @@ public class DataChannel {
}
}
private final long nativeDataChannel;
private long nativeDataChannel;
private long nativeObserver;
@CalledByNative
@ -112,6 +112,7 @@ public class DataChannel {
/** Register |observer|, replacing any previously-registered observer. */
public void registerObserver(Observer observer) {
checkDataChannelExists();
if (nativeObserver != 0) {
nativeUnregisterObserver(nativeObserver);
}
@ -120,18 +121,22 @@ public class DataChannel {
/** Unregister the (only) observer. */
public void unregisterObserver() {
checkDataChannelExists();
nativeUnregisterObserver(nativeObserver);
}
public String label() {
checkDataChannelExists();
return nativeLabel();
}
public int id() {
checkDataChannelExists();
return nativeId();
}
public State state() {
checkDataChannelExists();
return nativeState();
}
@ -141,16 +146,19 @@ public class DataChannel {
* to the network.
*/
public long bufferedAmount() {
checkDataChannelExists();
return nativeBufferedAmount();
}
/** Close the channel. */
public void close() {
checkDataChannelExists();
nativeClose();
}
/** Send |data| to the remote peer; return success. */
public boolean send(Buffer buffer) {
checkDataChannelExists();
// TODO(fischman): this could be cleverer about avoiding copies if the
// ByteBuffer is direct and/or is backed by an array.
byte[] data = new byte[buffer.data.remaining()];
@ -160,7 +168,9 @@ public class DataChannel {
/** Dispose of native resources attached to this channel. */
public void dispose() {
checkDataChannelExists();
JniCommon.nativeReleaseRef(nativeDataChannel);
nativeDataChannel = 0;
}
@CalledByNative
@ -168,6 +178,12 @@ public class DataChannel {
return nativeDataChannel;
}
private void checkDataChannelExists() {
if (nativeDataChannel == 0) {
throw new IllegalStateException("DataChannel has been disposed.");
}
}
private native long nativeRegisterObserver(Observer observer);
private native void nativeUnregisterObserver(long observer);
private native String nativeLabel();