From 895ce96c614f15d8761479b0ed54ce462b115a15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 21 Feb 2023 16:58:45 -0500 Subject: [PATCH] Bytecode: fix memory management for FBEGINBLOCK and FCALL tags FCALL was not properly deallocated, hence leading to a memory leak. Also simplify the handling of FBEGINBLOCK through a std::unique_ptr. --- mex/sources/bytecode/Evaluate.hh | 11 +++++++---- mex/sources/bytecode/Interpreter.cc | 11 +++-------- mex/sources/bytecode/Interpreter.hh | 2 +- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/mex/sources/bytecode/Evaluate.hh b/mex/sources/bytecode/Evaluate.hh index f7a9cf7ca..5acec84d6 100644 --- a/mex/sources/bytecode/Evaluate.hh +++ b/mex/sources/bytecode/Evaluate.hh @@ -24,6 +24,7 @@ #include #include #include +#include #include "Bytecode.hh" #include "ErrorHandling.hh" @@ -40,6 +41,8 @@ private: char *code; int nb_blocks; vector begin_block; + // Owns read instructions that have their specialized deserializing constructors + vector> deserialized_special_instrs; public: int get_block_number() const @@ -284,9 +287,9 @@ public: mexPrintf("FBEGINBLOCK\n"); # endif { - auto *fbegin_block = new FBEGINBLOCK_{code}; + deserialized_special_instrs.push_back(make_unique(code)); begin_block.push_back(tags_liste.size()); - tags_liste.push_back(fbegin_block); + tags_liste.push_back(deserialized_special_instrs.back().get()); nb_blocks++; } break; @@ -309,8 +312,8 @@ public: # ifdef DEBUGL mexPrintf("FCALL\n"); # endif - auto *fcall = new FCALL_{code}; - tags_liste.push_back(fcall); + deserialized_special_instrs.push_back(make_unique(code)); + tags_liste.push_back(deserialized_special_instrs.back().get()); # ifdef DEBUGL mexPrintf("FCALL finish\n"); mexEvalString("drawnow;"); mexPrintf("-- *code=%d\n", *code); mexEvalString("drawnow;"); diff --git a/mex/sources/bytecode/Interpreter.cc b/mex/sources/bytecode/Interpreter.cc index 91ff46797..da89bbfda 100644 --- a/mex/sources/bytecode/Interpreter.cc +++ b/mex/sources/bytecode/Interpreter.cc @@ -599,7 +599,7 @@ Interpreter::check_for_controlled_exo_validity(FBEGINBLOCK_ *fb, const vector &sconstrained_extended_path, const vector_table_conditional_local_type &vector_table_conditional_local) +Interpreter::MainLoop(const string &bin_basename, const CodeLoad &code, bool evaluate, int block, bool constrained, const vector &sconstrained_extended_path, const vector_table_conditional_local_type &vector_table_conditional_local) { int var; Block_Count = -1; @@ -698,8 +698,6 @@ Interpreter::MainLoop(const string &bin_basename, const CodeLoad &code, bool eva if (result == ERROR_ON_EXIT) return ERROR_ON_EXIT; } - if (last_call) - delete fb; } if (block >= 0) go_on = false; @@ -871,10 +869,7 @@ Interpreter::extended_path(const string &file_name, bool evaluate, int block, in vector_table_conditional_local.clear(); if (auto it = table_conditional_global.find(t); it != table_conditional_global.end()) vector_table_conditional_local = it->second; - if (t < nb_periods) - MainLoop(file_name, code, evaluate, block, false, true, sconstrained_extended_path, vector_table_conditional_local); - else - MainLoop(file_name, code, evaluate, block, true, true, sconstrained_extended_path, vector_table_conditional_local); + MainLoop(file_name, code, evaluate, block, true, sconstrained_extended_path, vector_table_conditional_local); for (int j = 0; j < y_size; j++) { y_save[j + (t + y_kmin) * y_size] = y[j + y_kmin * y_size]; @@ -926,7 +921,7 @@ Interpreter::compute_blocks(const string &file_name, bool evaluate, int block, i vector s_plan_junk; vector_table_conditional_local_type vector_table_conditional_local_junk; - MainLoop(file_name, code, evaluate, block, true, false, s_plan_junk, vector_table_conditional_local_junk); + MainLoop(file_name, code, evaluate, block, false, s_plan_junk, vector_table_conditional_local_junk); mxFree(*Init_Code); nb_blocks = Block_Count+1; diff --git a/mex/sources/bytecode/Interpreter.hh b/mex/sources/bytecode/Interpreter.hh index c93a029db..308d2d7c0 100644 --- a/mex/sources/bytecode/Interpreter.hh +++ b/mex/sources/bytecode/Interpreter.hh @@ -58,7 +58,7 @@ public: bool extended_path(const string &file_name, bool evaluate, int block, int &nb_blocks, int nb_periods, const vector &sextended_path, const vector &sconstrained_extended_path, const vector &dates, const table_conditional_global_type &table_conditional_global); bool compute_blocks(const string &file_name, bool evaluate, int block, int &nb_blocks); void check_for_controlled_exo_validity(FBEGINBLOCK_ *fb, const vector &sconstrained_extended_path); - bool MainLoop(const string &bin_basename, const CodeLoad &code, bool evaluate, int block, bool last_call, bool constrained, const vector &sconstrained_extended_path, const vector_table_conditional_local_type &vector_table_conditional_local); + bool MainLoop(const string &bin_basename, const CodeLoad &code, bool evaluate, int block, bool constrained, const vector &sconstrained_extended_path, const vector_table_conditional_local_type &vector_table_conditional_local); void ReadCodeFile(const string &file_name, CodeLoad &code); inline mxArray *