move program objects deletion to Program destructor to tackle a mem leak

This commit is contained in:
Louis Rubet 2022-03-08 11:16:54 +01:00
parent 39c9a75c7e
commit b72a0d76d5
2 changed files with 5 additions and 20 deletions

View file

@ -5,7 +5,7 @@
//< language reserved keywords (allowed types are kKeyword, kBranch or kUndef)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type" // allow casting kBranch callbacks
#pragma GCC diagnostic ignored "-Wpedantic" // allow designated initializers
#pragma GCC diagnostic ignored "-Wpedantic" // allow designated initializers for keywords_
vector<Program::keyword_t> Program::keywords_{
// GENERAL
{kUndef, "", nullptr, "\nGENERAL"},
@ -224,7 +224,7 @@ RetValue Program::Run() {
err_ = kOk;
err_context_ = "";
// iterate commands
// iterate objects
for (size_t i = 0; (go_out == false) && (i < size());) {
Object* o = at(i);
switch (o->_type) {
@ -294,10 +294,6 @@ RetValue Program::Run() {
}
}
// free allocated
for (Object* o : *this) delete o;
local_heap_.clear();
return ret;
}
@ -585,12 +581,9 @@ RetValue Program::Parse(const string& entry) {
case kProgram: {
Program* p = new Program(stack_, heap_, this);
if (p->Parse(element.value) == kOk) {
// give a clean format to the program string
stringstream ss;
for (size_t i = 0; i < p->size(); i++)
if (i > 0)
ss << ' ' << p->at(i);
else
ss << p->at(i);
for (size_t i = 0; i < p->size(); i++) ss << ((i > 0) ? " " : "") << p->at(i);
p->value = ss.str();
push_back(p);
}

View file

@ -18,15 +18,6 @@ using mpfr::mpreal;
#include "object.h"
#include "stack.h"
// struct Program : Object {
// Program() : Object(kProgram) {}
// explicit Program(const string& value__) : Object(kProgram), value(value__) {}
// virtual Object* Clone() { return new Program(value); }
// virtual string Name() { return string("program"); }
// virtual ostream& Show(ostream& out) { return out << "«" << value << "»"; }
// string value;
// };
//< program class: the class containing a string parser, all the programs keywords, a stack for running the program
class Program : public deque<Object*>, public Lexer, public Object {
public:
@ -37,6 +28,7 @@ class Program : public deque<Object*>, public Lexer, public Object {
virtual ~Program() {
local_heap_.clear();
for (Object* obj : *this) delete obj;
clear();
}