Skip to content

Commit

Permalink
Fix memory leak in unit tests
Browse files Browse the repository at this point in the history
The created SDL_Surfaces were not destroyed. Use a smart pointer with custom
deleter to do that.
  • Loading branch information
dariusarnold committed Apr 5, 2023
1 parent b6454e4 commit 776fd79
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/tests/test_dmg_acid2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,25 @@ TEST_CASE("dmg_acid2") {

// Convert the framebuffer to an SDL_Surface to save it for convenient comparisons if the tests
// fail.
auto* actual_image = actual_framebuffer.to_surface();
SDL_SaveBMP(actual_image, "dmg-acid2-actual.bmp");
using unique_surface_t = std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)>;
unique_surface_t actual_image{actual_framebuffer.to_surface(), SDL_FreeSurface};
SDL_SaveBMP(actual_image.get(), "dmg-acid2-actual.bmp");

// Load the known-good screenshot from a file
auto* expected_image = SDL_LoadBMP("recorded-logs/dmg-acid2.bmp");
expected_image = SDL_ConvertSurface(expected_image, actual_image->format, 0);
const unique_surface_t expected_image_load{SDL_LoadBMP("recorded-logs/dmg-acid2.bmp"),
SDL_FreeSurface};
unique_surface_t expected_image{
SDL_ConvertSurface(expected_image_load.get(), actual_image->format, 0), SDL_FreeSurface};
REQUIRE(expected_image != nullptr);
Framebuffer<graphics::gb::ColorScreen, constants::SCREEN_RES_WIDTH,
constants::SCREEN_RES_HEIGHT>
result;
auto res = SDL_LockSurface(expected_image);
auto res = SDL_LockSurface(expected_image.get());
REQUIRE(res == 0);
REQUIRE(expected_image->h * expected_image->pitch
== result.size() * sizeof(graphics::gb::ColorScreen));
result.take_from(expected_image->pixels);
SDL_UnlockSurface(expected_image);
SDL_UnlockSurface(expected_image.get());
// Compare the actual screenshot to the known-good one
for (size_t x = 0; x < result.width(); ++x) {
for (size_t y = 0; y < result.height(); ++y) {
Expand Down

0 comments on commit 776fd79

Please sign in to comment.