Fix memory leak when disposing a high-res Bitmap

This commit is contained in:
Splendide Imaginarius 2024-07-02 22:51:18 +00:00
parent 8fcd6dd605
commit 83647b947d
3 changed files with 31 additions and 0 deletions

View file

@ -805,6 +805,8 @@ Bitmap::Bitmap(SDL_Surface *imgSurf, SDL_Surface *imgSurfHires, bool forceMega)
Bitmap::~Bitmap()
{
dispose();
loresDispCon.disconnect();
}
void Bitmap::initFromSurface(SDL_Surface *imgSurf, Bitmap *hiresBitmap, bool forceMega)
@ -897,6 +899,7 @@ void Bitmap::setLores(Bitmap *lores) {
guardDisposed();
p->selfLores = lores;
loresDispCon = lores->wasDisposed.connect(&Bitmap::loresDisposal, this);
}
bool Bitmap::isMega() const{
@ -2603,3 +2606,9 @@ void Bitmap::releaseResources()
delete p;
}
void Bitmap::loresDisposal()
{
loresDispCon.disconnect();
dispose();
}

View file

@ -171,9 +171,12 @@ public:
private:
void releaseResources();
sigslot::connection loresDispCon;
const char *klassName() const { return "bitmap"; }
BitmapPrivate *p;
void loresDisposal();
};
#endif // BITMAP_H

View file

@ -395,6 +395,25 @@ dump(bmp, spr, "draw-text-green-outline")
# TODO: Animation tests, if we can find a good way to test them.
bmp = Bitmap.new(640, 480)
fnt = Font.new("Liberation Sans", 100)
fnt.out_color = Color.new(0, 255, 0)
bmp.font = fnt
bmp.draw_text(100, 200, 450, 300, "Now testing dispose for memory leaks...", 1)
dump(bmp, spr, "memory-leak")
def allocate()
bmp_allocate = Bitmap.new("Graphics/Pictures/children-alpha")
bmp_allocate.dispose
end
for i in (1...100)
for j in (1...10)
allocate
end
System::puts("Memory leak test #{i}/100")
end
# Tests are finished, show exit screen
bmp = Bitmap.new(640, 480)