简体   繁体   中英

Comparing weak_ptr to raw pointer doesn't work, looking for alternative

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?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM