Skip to content

Commit

Permalink
Merge pull request Ancurio#198 from Splendide-Imaginarius/mkxp-z-memo…
Browse files Browse the repository at this point in the history
…ry-leak-fix

Fix memory leak when disposing a high-res Bitmap
  • Loading branch information
Splendide-Imaginarius authored Jul 15, 2024
2 parents 67b9853 + 83647b9 commit 3376730
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 @@ -831,6 +831,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 @@ -927,6 +929,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 @@ -2635,3 +2638,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 3376730

Please sign in to comment.