Skip to content

Commit

Permalink
Fix memory leak when disposing a high-res Bitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
Splendide-Imaginarius committed Jul 2, 2024
1 parent 8fcd6dd commit 83647b9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/display/bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -897,6 +899,7 @@ void Bitmap::setLores(Bitmap *lores) {
guardDisposed();

p->selfLores = lores;
loresDispCon = lores->wasDisposed.connect(&Bitmap::loresDisposal, this);
}

bool Bitmap::isMega() const{
Expand Down Expand Up @@ -2603,3 +2606,9 @@ void Bitmap::releaseResources()

delete p;
}

void Bitmap::loresDisposal()
{
loresDispCon.disconnect();
dispose();
}
3 changes: 3 additions & 0 deletions src/display/bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,12 @@ class Bitmap : public Disposable

private:
void releaseResources();
sigslot::connection loresDispCon;
const char *klassName() const { return "bitmap"; }

BitmapPrivate *p;

void loresDisposal();
};

#endif // BITMAP_H
19 changes: 19 additions & 0 deletions tests/hires-bitmap/hires-bitmap-test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,25 @@ def dump(bmp, spr, desc)

# 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)
Expand Down

0 comments on commit 83647b9

Please sign in to comment.