Question

I have a SpriteManager class that loads and caches sprites for me, and removes unused sprites from the cache. That's the idea anyways, I'm a bit stuck. I have a map<string,weak_ptr<ALLEGRO_BITMAP>> where I'm storing the sprites, and use the weak_ptr to spawn off shared_ptr's. Now I'm trying to use a deleter that also removes the bitmap from the map, it looks like this (not working, obviously):

[&bitmaps](ALLEGRO_BITMAP* bmp){
        for(auto it = bitmaps.begin(); it!=bitmaps.end(); ++it) {
            if((*it).second == bmp) {
                bitmaps.erase(it);
                al_destroy_bitmap(bmp);
                break;
            }
        }
}

bitmaps being the map I was talking about. Of course I can't compare (*it).second and bmp, but I also can't lock the weak_ptr because I'm in the deleter. Do I really have no other choice other than to keep both the weak and the raw pointer around?

Était-ce utile?

La solution

Store iterator to the weak_ptr in the map in the deleter along with &bitmaps. then remove with it.

[&bitmaps, iter](ALLEGRO_BITMAP* bmp){
    bitmaps.erase(iter);
    al_destroy_bitmap(bmp);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top