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

This is a reland of 8a959bfa88b08e215baf3b38e914c41e483c9ece

Original change's description:
> 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}

Bug: None
Change-Id: If8fb02ca149257dd29b0c3352347369168a5cef3
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/142807
Reviewed-by: Magnus Jedvert <magjed@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28372}
This commit is contained in:
Niels Möller
2019-06-18 16:54:36 +02:00
committed by Commit Bot
parent 3894078d60
commit f03b365875
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();
}
}