Brian Batista
Brian Batista

Reputation: 67

How to controll shared ptr reference count?

I'm creating a Resource Manager for my game engine. Basically it have an unordered_map that stores the path of the resource as key and as value it stores the loaded resource.

This is the code responsible for loading assets.

std::shared_ptr<T> LoadAsset(const String& _path)
{
    if (auto asset = m_assets.find(_path); asset != m_assets.end()) {
        return std::static_pointer_cast<T>(asset->second);
    }

    auto asset = std::move(LoadAssetInternal(_path));
    if (!asset) {
        return nullptr;
    }

    m_assets[_path] = std::move(asset);

    return std::static_pointer_cast<T>(m_assets[_path]);
}

The problem is that when I call the LoadAsset method, the returned shared_ptr variable always has 2 strong ref when I delete the variable holding the resource the ref count goes to 1 and the resource is never freed by the end of the program.

Exemple:

auto tex = LoadAsset<Texture>("Data/Textures/Foo.tga"); // Strong refs = 2
tex = nullptr; // Strong refs = 1 and the loaded Foo.tga is never freed.

Upvotes: 1

Views: 107

Answers (1)

Girspoon
Girspoon

Reputation: 96

Just create a function that runs at the end of your games main loop. Something like this

void PurgeAssets() {
    for (auto i = m_assets.begin(); i != m_assets.end();) {
        if (i->second.unique()) {
            i = m_assets.erase(i);
        }
        else {
            ++i;
        }
    }
}

Upvotes: 1

Related Questions