From 83647b947d079f778283182161e40f9194aed9b6 Mon Sep 17 00:00:00 2001 From: Splendide Imaginarius <119545140+Splendide-Imaginarius@users.noreply.github.com> Date: Tue, 2 Jul 2024 22:51:18 +0000 Subject: [PATCH] Fix memory leak when disposing a high-res Bitmap --- src/display/bitmap.cpp | 9 +++++++++ src/display/bitmap.h | 3 +++ tests/hires-bitmap/hires-bitmap-test.rb | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/src/display/bitmap.cpp b/src/display/bitmap.cpp index 67bab505b..6af25d082 100644 --- a/src/display/bitmap.cpp +++ b/src/display/bitmap.cpp @@ -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(); +} diff --git a/src/display/bitmap.h b/src/display/bitmap.h index c6082fc8c..a41f271ff 100644 --- a/src/display/bitmap.h +++ b/src/display/bitmap.h @@ -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 diff --git a/tests/hires-bitmap/hires-bitmap-test.rb b/tests/hires-bitmap/hires-bitmap-test.rb index 3a62d0f9d..ba044cb10 100755 --- a/tests/hires-bitmap/hires-bitmap-test.rb +++ b/tests/hires-bitmap/hires-bitmap-test.rb @@ -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)