Bytecode: fix memory leak in Evaluate class

The newly-created FBEGINBLOCK_ and FCALL_ instances were freed using a
base-class pointer. But the latter does not have a virtual destructor.

Those class instances are now stored by value in containers, so that the
destructor of the derived class is used.
kalman-mex
Sébastien Villemot 2023-09-01 13:48:31 +02:00
parent 9e5bd75611
commit bd9943a695
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
3 changed files with 13 additions and 8 deletions

View File

@ -229,10 +229,10 @@ Evaluate::Evaluate(const filesystem::path &codfile, bool steady_state_arg, const
# ifdef DEBUGL
mexPrintf("FBEGINBLOCK\n");
# endif
deserialized_special_instrs.push_back(make_unique<FBEGINBLOCK_>(code));
deserialized_fbeginblock.emplace_back(code);
begin_block.push_back(instructions_list.size());
nb_blocks++;
instr = deserialized_special_instrs.back().get();
instr = &deserialized_fbeginblock.back();
break;
case Tags::FJMPIFEVAL:
# ifdef DEBUGL
@ -250,8 +250,8 @@ Evaluate::Evaluate(const filesystem::path &codfile, bool steady_state_arg, const
# ifdef DEBUGL
mexPrintf("FCALL\n");
# endif
deserialized_special_instrs.push_back(make_unique<FCALL_>(code));
instr = deserialized_special_instrs.back().get();
deserialized_fcall.emplace_back(code);
instr = &deserialized_fcall.back();
break;
case Tags::FLDTEF:
# ifdef DEBUGL

View File

@ -26,6 +26,7 @@
#include <optional>
#include <memory>
#include <filesystem>
#include <deque>
#include "Bytecode.hh"
#include "BasicSymbolTable.hh"
@ -43,11 +44,15 @@ private:
unique_ptr<char[]> raw_bytecode;
/* Owns read instructions that have their specialized deserializing
constructors (and are thus not part of the code memory block) */
vector<unique_ptr<BytecodeInstruction>> deserialized_special_instrs;
constructors (and are thus not part of the code memory block). We use
std::deque for storing them, because that class guarantees the stability
of iterators, and thus of pointers to elements; we store such pointers in
the instructions_list data member. */
deque<FBEGINBLOCK_> deserialized_fbeginblock;
deque<FCALL_> deserialized_fcall;
/* List of deserialized instructions
Those are either pointers inside raw_bytecode or deserialized_special_instrs */
Those are either pointers inside raw_bytecode or deserialized_{fbeginblock,fcall} */
instructions_list_t instructions_list;
// Number of blocks in the model

@ -1 +1 @@
Subproject commit 92f42bdf68bbdb217647e5268dc91cc7cf05df87
Subproject commit 3a187076859751ebc3f6a4e43c9ffc3149655191