In this tutorial that shows thread-safe methods for looping audio, it says:
void checkForBuffersToFree()
{
for (auto i = buffers.size(); --i >= 0;) // [1]
{
ReferenceCountedBuffer::Ptr buffer (buffers.getUnchecked (i)); // [2]
if (buffer->getReferenceCount() == 2) // [3]
buffers.remove (i);
}
}
- [1]: It is useful to remember to iterate over the array in reverse in these situations. It is easier to avoid corrupting the array index access if we remove items as we iterate over the array.
- [2]: This retains a copy of a buffer at the specified index.
- [3]: If the reference count at this point is equal to 2 then we know that the audio thread can’t be using the buffer and we can remove it from the array. One of these two references will be in the
buffersand the other will be in the localbuffervariable. The removed buffer will delete itself as thebuffervariable goes out of scope (as this will be the last remaining reference).
Question:
At [3], why/how does a reference count of 2 indicate that the audio thread is not using the buffer? It seems that making a copy of any buffer here that already exists would make the reference count 2…

