From f8eaea321eda1e84f49951463d744dfb62abcac0 Mon Sep 17 00:00:00 2001 From: adjoly Date: Mon, 30 Jun 2025 10:54:43 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20fixed?= =?UTF-8?q?=20leak=20when=20throwing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex00/BitcoinExchange.cpp | 14 ++++++++++---- ex00/Makefile | 4 ++-- ex00/main.cpp | 7 +++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/ex00/BitcoinExchange.cpp b/ex00/BitcoinExchange.cpp index 7b32a0e..dbe3fce 100644 --- a/ex00/BitcoinExchange.cpp +++ b/ex00/BitcoinExchange.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/21 10:39:45 by adjoly #+# #+# */ -/* Updated: 2025/06/03 11:40:22 by adjoly ### ########.fr */ +/* Updated: 2025/06/30 10:54:14 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -213,14 +213,18 @@ BitcoinExchange::BitcoinExchange(char *av) : _is(av), _filename(av) { throw e; } - if (!_is.is_open()) + if (!_is.is_open()) { + delete _csv; throw std::runtime_error("could not open file - " + _filename); + } std::string buf; std::getline(_is, buf); - if (buf != "date | value") + if (buf != "date | value") { + delete _csv; throw std::runtime_error("file has not the good format - " + buf); + } } BitcoinExchange::BitcoinExchange(const BitcoinExchange &) { @@ -291,8 +295,10 @@ std::pair BitcoinExchange::_parseLine(void) { void BitcoinExchange::_printPair(std::pair pair) { auto map = _csv->getCsv(); auto it = std::find_if(map.begin(), map.end(), CompareDate(pair.first)); - if (it == map.end()) + if (it == map.end()) { + delete _csv; throw std::out_of_range("out of range"); + } float i = it->second * pair.second; std::cout << pair.first << " => " << pair.second << " = " << i << std::endl; diff --git a/ex00/Makefile b/ex00/Makefile index 349a4b7..2d7972d 100644 --- a/ex00/Makefile +++ b/ex00/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/25 16:09:27 by adjoly #+# #+# # -# Updated: 2025/05/30 13:24:23 by adjoly ### ########.fr # +# Updated: 2025/06/30 10:51:07 by adjoly ### ########.fr # # # # **************************************************************************** # @@ -22,7 +22,7 @@ SRCS = $(shell find . -name '*.cpp') OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o)) -FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP +FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP -g RED = \033[0;31m GREEN = \033[0;32m diff --git a/ex00/main.cpp b/ex00/main.cpp index 711a82c..4f515da 100644 --- a/ex00/main.cpp +++ b/ex00/main.cpp @@ -6,11 +6,12 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/21 10:34:50 by adjoly #+# #+# */ -/* Updated: 2025/06/03 11:31:50 by adjoly ### ########.fr */ +/* Updated: 2025/06/30 10:52:15 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include +#include #include #include #include @@ -23,9 +24,11 @@ int main(int ac, char **av) { btc = new BitcoinExchange(av[1]); btc->print(); } catch (std::runtime_error &e) { + btc = NULL; std::cout << e.what() << std::endl; } - delete btc; + if (btc != NULL) + delete btc; } else { std::cout << "Error: could not open file." << std::endl; }