I use library Allegro5.
In my class named Animation I have vector<ALLEGRO_BITMAP*> sprites
.
In constructor, I populate it with some bitmaps and in destructor I would like to delete it properly. However, my solutions still lead to memory leaking:
for each (ALLEGRO_BITMAP* it in sprites)
{
al_destroy_bitmap(it);
}
for (auto it : sprites)
delete it;
sprites.~vector();
or any other combination of those.
I really can't find out why it isn't deleting properly.
EDIT:
Now I have this and there is still a small memory leak, but in the size less then 1 MB
class Deleter
{
public:
void operator()(ALLEGRO_BITMAP* ptrToBitmap)
{
cout << "deleted"<<'\n';
al_destroy_bitmap(ptrToBitmap);
}
};
and
vector<unique_ptr<ALLEGRO_BITMAP, Deleter>> sprites;
for (int i = 0; i < 100; i++)
{
ALLEGRO_BITMAP* temp = al_load_bitmap("Fireball1.png");
unique_ptr<ALLEGRO_BITMAP, Deleter> uptr;
uptr = unique_ptr<ALLEGRO_BITMAP, Deleter>(temp);
sprites.push_back(move(uptr));
}
sprites.~vector();
Don't make sprites
a std::vector<ALLEGRO_BITMAP*>
, make it a std::vector<std::unique_ptr<ALLEGRO_BITMAP*,al_destroy_bitmap>>
. That way, when the destructor of sprites
is called (or when you resize it to zero), the unique_ptr destructor will free the bitmap.