kcat / alure

Alure is a utility library for OpenAL, providing a C++ API and managing common tasks that include file loading, caching, and streaming
zlib License
70 stars 20 forks source link

Bug in context.cpp #7

Closed Silverlan closed 7 years ago

Silverlan commented 7 years ago

// Edit: Nevermind this, I've created a pull request instead.

There's a small (but with severe consequences) bug in context.cpp in the function ALContext::createSource():

Source *ALContext::createSource()
{
    CheckContext(this);

    ALSource *source;
    if(!mFreeSources.empty())
    {
        source = mFreeSources.back(); // Bug!
        mFreeSources.pop();
    }
    else
    {
        mAllSources.emplace_back(this);
        source = &mAllSources.back();
    }
    auto iter = std::lower_bound(mUsedSources.begin(), mUsedSources.end(), source);
    if(iter == mUsedSources.end() || *iter != source)
        mUsedSources.insert(iter, source);
    return source;
}

mFreeSources is a queue, which means pop removes the first element from it. However, it retrieves the last element from the queue before popping it. That means source now points to an object still in the queue, and the element which was popped is now dangling. It should be:

source = mFreeSources.front();