1
0

🔨」 fix: fixed leak when throwing

This commit is contained in:
2025-06-30 10:54:43 +02:00
parent da635ef59a
commit f8eaea321e
3 changed files with 17 additions and 8 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/21 10:39:45 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; throw e;
} }
if (!_is.is_open()) if (!_is.is_open()) {
delete _csv;
throw std::runtime_error("could not open file - " + _filename); throw std::runtime_error("could not open file - " + _filename);
}
std::string buf; std::string buf;
std::getline(_is, 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); throw std::runtime_error("file has not the good format - " + buf);
}
} }
BitcoinExchange::BitcoinExchange(const BitcoinExchange &) { BitcoinExchange::BitcoinExchange(const BitcoinExchange &) {
@ -291,8 +295,10 @@ std::pair<Date, float> BitcoinExchange::_parseLine(void) {
void BitcoinExchange::_printPair(std::pair<Date, float> pair) { void BitcoinExchange::_printPair(std::pair<Date, float> pair) {
auto map = _csv->getCsv(); auto map = _csv->getCsv();
auto it = std::find_if(map.begin(), map.end(), CompareDate(pair.first)); 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"); throw std::out_of_range("out of range");
}
float i = it->second * pair.second; float i = it->second * pair.second;
std::cout << pair.first << " => " << pair.second << " = " << i << std::endl; std::cout << pair.first << " => " << pair.second << " = " << i << std::endl;

View File

@ -6,7 +6,7 @@
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ # # By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 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)) 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 RED = \033[0;31m
GREEN = \033[0;32m GREEN = \033[0;32m

View File

@ -6,11 +6,12 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/21 10:34:50 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 <BitcoinExchange.hpp> #include <BitcoinExchange.hpp>
#include <cstddef>
#include <cstdlib> #include <cstdlib>
#include <exception> #include <exception>
#include <stdexcept> #include <stdexcept>
@ -23,9 +24,11 @@ int main(int ac, char **av) {
btc = new BitcoinExchange(av[1]); btc = new BitcoinExchange(av[1]);
btc->print(); btc->print();
} catch (std::runtime_error &e) { } catch (std::runtime_error &e) {
btc = NULL;
std::cout << e.what() << std::endl; std::cout << e.what() << std::endl;
} }
delete btc; if (btc != NULL)
delete btc;
} else { } else {
std::cout << "Error: could not open file." << std::endl; std::cout << "Error: could not open file." << std::endl;
} }