Close all memory leaks.

This commit is contained in:
Stefano Sanfilippo 2014-11-27 23:52:13 +01:00
parent 67e5d3a4be
commit 84199887a7
2 changed files with 33 additions and 1 deletions

View File

@ -6,6 +6,9 @@ extern int yylex();
#include "Nodes.hpp" #include "Nodes.hpp"
// For free()
#include <cstdlib>
using namespace monicelli; using namespace monicelli;
extern Program *program; extern Program *program;
@ -128,6 +131,7 @@ fun_decl:
$$ = new Function(new Id($2), paramsStack.top(), stmtStack.top()); $$ = new Function(new Id($2), paramsStack.top(), stmtStack.top());
paramsStack.pop(); paramsStack.pop();
stmtStack.pop(); stmtStack.pop();
free($2);
} }
; ;
args: args:
@ -189,7 +193,14 @@ numeric:
NUMBER { $$ = new Integer($1); } | FLOAT { $$ = new Float($1); } NUMBER { $$ = new Integer($1); } | FLOAT { $$ = new Float($1); }
; ;
variable: variable:
ID { $$ = new Id($1); } | ARTICLE ID { $$ = new Id($2); } ID {
$$ = new Id($1);
free($1);
}
| ARTICLE ID {
$$ = new Id($2);
free($2);
}
; ;
assign_stmt: assign_stmt:
variable ASSIGN expression { variable ASSIGN expression {
@ -262,6 +273,7 @@ fun_call:
ID call_args FUN_END { ID call_args FUN_END {
$$ = new FunctionCall(new Id($3), argsStack.top()); $$ = new FunctionCall(new Id($3), argsStack.top());
argsStack.pop(); argsStack.pop();
free($3);
} }
; ;
call_args: call_args:

View File

@ -23,19 +23,31 @@ public:
class Statement: public Emittable { class Statement: public Emittable {
public:
virtual ~Statement() {}
}; };
class SemiExpression: public Emittable { class SemiExpression: public Emittable {
public:
virtual ~SemiExpression() {}
}; };
class Expression: public Emittable { class Expression: public Emittable {
public:
virtual ~Expression() {}
}; };
class StatementList: public std::vector<Statement*>, public Emittable { class StatementList: public std::vector<Statement*>, public Emittable {
public: public:
virtual ~StatementList() {
for (Statement *s: *this) {
delete s;
}
}
virtual void emit(std::ostream &stream, int indent = 0); virtual void emit(std::ostream &stream, int indent = 0);
}; };
@ -55,6 +67,12 @@ public:
} }
} }
virtual ~ListEmittable() {
for (T e: *this) {
delete e;
}
}
protected: protected:
virtual std::string getSeparator() const { virtual std::string getSeparator() const {
return ", "; return ", ";
@ -73,6 +91,8 @@ class SimpleExpression: public Expression {
class Id: public SimpleExpression { class Id: public SimpleExpression {
public: public:
explicit Id(const char *c): value(c) {} explicit Id(const char *c): value(c) {}
virtual ~Id() {}
virtual void emit(std::ostream &stream, int indent = 0); virtual void emit(std::ostream &stream, int indent = 0);
private: private: