From 84199887a7e6f1d1d8c615ce152ec1c6aea60240 Mon Sep 17 00:00:00 2001 From: Stefano Sanfilippo Date: Thu, 27 Nov 2014 23:52:13 +0100 Subject: [PATCH] Close all memory leaks. --- Monicelli.y | 14 +++++++++++++- Nodes.hpp | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Monicelli.y b/Monicelli.y index 77d7cd3..5471f84 100644 --- a/Monicelli.y +++ b/Monicelli.y @@ -6,6 +6,9 @@ extern int yylex(); #include "Nodes.hpp" +// For free() +#include + using namespace monicelli; extern Program *program; @@ -128,6 +131,7 @@ fun_decl: $$ = new Function(new Id($2), paramsStack.top(), stmtStack.top()); paramsStack.pop(); stmtStack.pop(); + free($2); } ; args: @@ -189,7 +193,14 @@ numeric: NUMBER { $$ = new Integer($1); } | FLOAT { $$ = new Float($1); } ; 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: variable ASSIGN expression { @@ -262,6 +273,7 @@ fun_call: ID call_args FUN_END { $$ = new FunctionCall(new Id($3), argsStack.top()); argsStack.pop(); + free($3); } ; call_args: diff --git a/Nodes.hpp b/Nodes.hpp index d26ee34..2bcab38 100644 --- a/Nodes.hpp +++ b/Nodes.hpp @@ -23,19 +23,31 @@ public: class Statement: public Emittable { +public: + virtual ~Statement() {} }; class SemiExpression: public Emittable { +public: + virtual ~SemiExpression() {} }; class Expression: public Emittable { +public: + virtual ~Expression() {} }; class StatementList: public std::vector, public Emittable { public: + virtual ~StatementList() { + for (Statement *s: *this) { + delete s; + } + } + virtual void emit(std::ostream &stream, int indent = 0); }; @@ -55,6 +67,12 @@ public: } } + virtual ~ListEmittable() { + for (T e: *this) { + delete e; + } + } + protected: virtual std::string getSeparator() const { return ", "; @@ -73,6 +91,8 @@ class SimpleExpression: public Expression { class Id: public SimpleExpression { public: explicit Id(const char *c): value(c) {} + virtual ~Id() {} + virtual void emit(std::ostream &stream, int indent = 0); private: