Raise IllegalStateException for calls to retain() or release() on zero ref count

Bug: None
Change-Id: I3205e77b5adfdc4f5dbd7509d1ca0e8b08af62f2
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/142175
Commit-Queue: Niels Moller <nisse@webrtc.org>
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28319}
This commit is contained in:
Niels Möller
2019-06-18 16:54:36 +02:00
committed by Commit Bot
parent 505bac209b
commit 8a959bfa88
2 changed files with 46 additions and 18 deletions

View File

@ -29,12 +29,19 @@ class RefCountDelegate implements RefCounted {
@Override
public void retain() {
refCount.incrementAndGet();
int updated_count = refCount.incrementAndGet();
if (updated_count < 2) {
throw new IllegalStateException("retain() called on an object with refcount < 1");
}
}
@Override
public void release() {
if (refCount.decrementAndGet() == 0 && releaseCallback != null) {
int updated_count = refCount.decrementAndGet();
if (updated_count < 0) {
throw new IllegalStateException("release() called on an object with refcount < 1");
}
if (updated_count == 0 && releaseCallback != null) {
releaseCallback.run();
}
}