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.
silicon
Sébastien Villemot 2023-02-21 16:58:45 -05:00
parent 1ad6b29936
commit 895ce96c61
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
3 changed files with 11 additions and 13 deletions

View File

@ -24,6 +24,7 @@
#include <string>
#include <map>
#include <optional>
#include <memory>
#include "Bytecode.hh"
#include "ErrorHandling.hh"
@ -40,6 +41,8 @@ private:
char *code;
int nb_blocks;
vector<size_t> begin_block;
// Owns read instructions that have their specialized deserializing constructors
vector<unique_ptr<BytecodeInstruction>> 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<FBEGINBLOCK_>(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<FCALL_>(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;");

View File

@ -599,7 +599,7 @@ Interpreter::check_for_controlled_exo_validity(FBEGINBLOCK_ *fb, const vector<s_
}
bool
Interpreter::MainLoop(const string &bin_basename, const CodeLoad &code, bool evaluate, int block, bool last_call, bool constrained, const vector<s_plan> &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<s_plan> &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> 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;

View File

@ -58,7 +58,7 @@ public:
bool extended_path(const string &file_name, bool evaluate, int block, int &nb_blocks, int nb_periods, const vector<s_plan> &sextended_path, const vector<s_plan> &sconstrained_extended_path, const vector<string> &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<s_plan> &sconstrained_extended_path);
bool MainLoop(const string &bin_basename, const CodeLoad &code, bool evaluate, int block, bool last_call, bool constrained, const vector<s_plan> &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<s_plan> &sconstrained_extended_path, const vector_table_conditional_local_type &vector_table_conditional_local);
void ReadCodeFile(const string &file_name, CodeLoad &code);
inline mxArray *