@@ -72,11 +72,15 @@ public boolean decRef() {
7272
7373 @ Override
7474 public boolean hasReferences () {
75- return refCounted .hasReferences ();
75+ boolean hasRef = refCounted .hasReferences ();
76+ // delegate is nulled out when the ref-count reaches zero but only via a plain store, and also we could be racing with a concurrent
77+ // decRef so need to check #refCounted again in case we run into a non-null delegate but saw a reference before
78+ assert delegate != null || hasRef == false || refCounted .hasReferences () == false ;
79+ return hasRef ;
7680 }
7781
7882 public ReleasableBytesReference retain () {
79- refCounted .incRef ();
83+ refCounted .mustIncRef ();
8084 return this ;
8185 }
8286
@@ -86,6 +90,7 @@ public ReleasableBytesReference retain() {
8690 * retaining unnecessary buffers.
8791 */
8892 public ReleasableBytesReference retainedSlice (int from , int length ) {
93+ assert hasReferences ();
8994 if (from == 0 && length () == length ) {
9095 return retain ();
9196 }
@@ -140,6 +145,7 @@ public int indexOf(byte marker, int from) {
140145
141146 @ Override
142147 public int length () {
148+ assert hasReferences ();
143149 return delegate .length ();
144150 }
145151
@@ -158,6 +164,7 @@ public ReleasableBytesReference slice(int from, int length) {
158164
159165 @ Override
160166 public long ramBytesUsed () {
167+ assert hasReferences ();
161168 return delegate .ramBytesUsed ();
162169 }
163170
@@ -232,6 +239,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
232239
233240 @ Override
234241 public boolean isFragment () {
242+ assert hasReferences ();
235243 return delegate .isFragment ();
236244 }
237245
0 commit comments