From 0b72fabadec642dcecfe861c56dfeddb0c82a898 Mon Sep 17 00:00:00 2001 From: scratko Date: Thu, 21 Nov 2024 00:42:10 +0300 Subject: Fixed memory leak The image widget is responsible for deleting the loaded part of the image Check for correct type of selected file Error checking after image upload --- puzzle.cpp | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'puzzle.cpp') diff --git a/puzzle.cpp b/puzzle.cpp index 3fbd026..8cca327 100644 --- a/puzzle.cpp +++ b/puzzle.cpp @@ -4,13 +4,15 @@ #include #include #include +#include +#include #include +#include #include #include #include #include - GameParams* GameParams::instance = nullptr; void GameParams::CalculateStandardPuzzlePos() @@ -47,8 +49,10 @@ void GameParams::NextUntestedPuzzles() /* * check if puzzles already were created */ - if(puzzles.size() != 0) + if(puzzles.size() != 0) { puzzles.clear(); + Fl::do_widget_deletion(); + } /* * ======== creating puzzles =========== */ @@ -74,13 +78,33 @@ void GameParams::NextUntestedPuzzles() find_path_to_picture(tmp_puzzle->path, cur_directory, tmp_puzzle->sequence_number); Fl_PNG_Image *img = new Fl_PNG_Image(tmp_puzzle->path.c_str()); - // TODO check fails img->fail(); + switch (img->fail()) { + case Fl_Image::ERR_NO_IMAGE: + case Fl_Image::ERR_FILE_ACCESS: + fl_alert("%s: %s", tmp_puzzle->path.c_str(), strerror(errno)); + exit(1); + case Fl_Image::ERR_FORMAT: + fl_alert("couldn't decode image"); + exit(1); + } tmp_puzzle->image(img); + tmp_puzzle->stored_img_pointer = img; tmp_puzzle->callback(press_button_callback, this); puzzles.push_back(std::move(tmp_puzzle)); } - win->end(); ResetFreePuzzles(); + /* + * Checking image shuffling + */ + bool shuffled_img = false; + for(int i = 0; i < puzzle_pieces-1; ++i) + if(puzzles[i]->sequence_number != i) { + shuffled_img = true; + break; + } + if(!shuffled_img) + NextUntestedPuzzles(); + win->end(); } bool GameParams::IsSolvability() -- cgit v1.2.3