From 7a6275c0a1ff61216b0dc7f2f3a78e5e958b5ead Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Mon, 15 Apr 2024 05:34:02 +0630 Subject: fix memory leak through vec.clear vec.clear() doesn't delete pointers (unless they're smart) so I need to do it myself. --- asm/preprocesser.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'asm/preprocesser.cpp') diff --git a/asm/preprocesser.cpp b/asm/preprocesser.cpp index 6971d2b..0976396 100644 --- a/asm/preprocesser.cpp +++ b/asm/preprocesser.cpp @@ -20,6 +20,12 @@ using std::pair, std::vector, std::make_pair, std::string, std::string_view; #define ERR(E) std::make_pair(tokens, (E)) #define VAL(E) std::make_pair(E, pp_err_t{pp_err_type_t::OK}) +#define VCLEAR(V) \ + std::for_each((V).begin(), (V).end(), \ + [](token_t *t) \ + { \ + delete t; \ + }); pair, pp_err_t> preprocesser(vector &tokens) { @@ -33,12 +39,12 @@ pair, pp_err_t> preprocesser(vector &tokens) std::tie(const_block_tokens, pperr) = preprocess_const_blocks(tokens); if (pperr.type != pp_err_type_t::OK) { - tokens.clear(); + VCLEAR(tokens); tokens = use_block_tokens; return ERR(pperr); } - use_block_tokens.clear(); + VCLEAR(use_block_tokens); return VAL(const_block_tokens); } @@ -54,7 +60,7 @@ preprocess_use_blocks(const vector &tokens) if (i + 1 >= tokens.size() || tokens[i + 1]->type != token_type_t::LITERAL_STRING) { - new_tokens.clear(); + VCLEAR(new_tokens); return ERR(pp_err_t(pp_err_type_t::EXPECTED_STRING, t)); } @@ -62,7 +68,7 @@ preprocess_use_blocks(const vector &tokens) auto source = read_file(name->content.c_str()); if (!source) { - new_tokens.clear(); + VCLEAR(new_tokens); return ERR(pp_err_t(pp_err_type_t::FILE_NONEXISTENT, name)); } @@ -70,7 +76,7 @@ preprocess_use_blocks(const vector &tokens) lerr_t lerr = tokenise_buffer(source.value(), ftokens); if (lerr != lerr_t::OK) { - new_tokens.clear(); + VCLEAR(new_tokens); return ERR(pp_err_t(pp_err_type_t::FILE_PARSE_ERROR, name, lerr)); } @@ -150,7 +156,7 @@ preprocess_const_blocks(vector const &tokens) auto it = blocks.find(token->content); if (it == blocks.end()) { - new_tokens.clear(); + VCLEAR(new_tokens); return ERR(pp_err_t(pp_err_type_t::UNKNOWN_NAME, token)); } -- cgit v1.2.3-13-gbd6f