Permalink
Browse files

fixed memory leaks

  • Loading branch information...
1 parent eb38f04 commit 76fad91f82affb2ac1d0eed8e4b172ac5e7b7fcb @tokuhirom committed Mar 23, 2012
Showing with 52 additions and 22 deletions.
  1. +1 −1 misc/valgrind.pl
  2. +32 −0 tests/test_value_op.cc
  3. +6 −7 tora/compiler.cc
  4. +2 −1 tora/compiler.h
  5. +1 −2 tora/op.h
  6. +1 −2 tora/pad_list.cc
  7. +1 −1 tora/peek.cc
  8. +2 −2 tora/value/code.cc
  9. +6 −6 tora/value/code.h
View
@@ -14,7 +14,7 @@
subtest $src => sub {
my ($in, $out, $err);
$err = gensym;
- my $pid = open3($in, $out, $err, "valgrind ./tora $src");
+ my $pid = open3($in, $out, $err, "valgrind ./bin/tora $src");
close $in;
my $ret = join('', <$out>);
my $err_ret = join('', <$err>);
@@ -0,0 +1,32 @@
+#include "nanotap.h"
+#include "../tora/op.h"
+#include "../tora/value.h"
+#include "../tora/symbol_table.h"
+#include "../tora/value/code.h"
+#include <stdarg.h>
+
+using namespace tora;
+
+int main() {
+ {
+ SharedPtr<SymbolTable> symbol_table = new SymbolTable();
+ SharedPtr<OPArray> op_array = new OPArray();
+ std::string filename("HOGE");
+ int lineno = 3;
+ boost::shared_ptr<std::vector<std::string>> params(new std::vector<std::string>());
+ SharedPtr<CodeValue> code(new CodeValue(
+ symbol_table->get_id("hgoe"), // package id
+ symbol_table->get_id("fuga"), // func name id
+ filename,
+ lineno,
+ params
+ ));
+ is(code->refcnt, 1);
+ SharedPtr<ValueOP> op(new ValueOP(OP_PUSH_VALUE, code.get()));
+ is(op->refcnt, 1);
+ }
+ ok(1);
+
+ done_testing();
+}
+
View
@@ -75,13 +75,12 @@ Compiler::Compiler(const SharedPtr<SymbolTable> &symbol_table_, const std::strin
symbol_table = symbol_table_;
dump_ops = false;
package_ = "main";
- closure_vars = new std::vector<std::string>();
+ closure_vars.reset(new std::vector<std::string>());
in_loop_context = false;
}
Compiler::~Compiler() {
delete global_vars;
delete blocks;
- delete closure_vars;
}
void Compiler::push_op(OP * op) {
@@ -529,7 +528,7 @@ void tora::Compiler::compile(const SharedPtr<Node> &node) {
);
assert(params);
code->code_opcodes(funccomp.ops);
- code->closure_var_names(new std::vector<std::string>(*funccomp.closure_vars));
+ code->closure_var_names(funccomp.closure_vars);
// if (funccomp.closure_vars->size() > 0) {
// create closure
@@ -636,19 +635,19 @@ void tora::Compiler::compile(const SharedPtr<Node> &node) {
// printf("CLOSURE VARS: %d\n", funccomp.closure_vars->size());
- SharedPtr<CodeValue> code = new CodeValue(
+ SharedPtr<CodeValue> code(new CodeValue(
this->symbol_table->get_id(package), // package id
this->symbol_table->get_id(funcname), // func name id
filename_,
node->lineno,
params
- );
+ ));
assert(params);
// code->code_id = this->symbol_table->get_id(package + "::" + funcname);
code->code_params(params);
code->code_defaults(defaults);
code->code_opcodes(funccomp.ops);
- code->closure_var_names(new std::vector<std::string>(*funccomp.closure_vars));
+ code->closure_var_names(funccomp.closure_vars);
SharedPtr<StrValue> funcname_value = new StrValue(funcname);
if (1 && funccomp.closure_vars->size() > 0) {
@@ -673,7 +672,7 @@ void tora::Compiler::compile(const SharedPtr<Node> &node) {
push_op(define_method);
}
- push_op(new ValueOP(OP_PUSH_VALUE, code.get()));
+ push_op(new ValueOP(OP_PUSH_VALUE, code));
break;
}
View
@@ -7,6 +7,7 @@
#include <stdarg.h>
#include <vector>
#include <string>
+#include <boost/shared_ptr.hpp>
namespace tora {
@@ -44,7 +45,7 @@ class Compiler {
SharedPtr<OPArray> ops;
std::vector<SharedPtr<Block>> *blocks;
std::vector<std::string> *global_vars;
- std::vector<std::string> *closure_vars;
+ boost::shared_ptr<std::vector<std::string>> closure_vars;
SharedPtr<SymbolTable> symbol_table;
int error;
bool in_try_block;
View
@@ -27,7 +27,7 @@ class OP {
operand.int_value = (((high)&0x0000ffff) << 16) | (low&0x0000ffff);
}
// This is not a virtual destructor. Do not define destructor in child class.
- ~OP() { }
+ virtual ~OP() { }
template<class Y>
Y* upcast() {
@@ -49,7 +49,6 @@ class ValueOP : public OP {
: OP(type)
, value(v) {
}
- ~ValueOP() { }
};
};
View
@@ -11,7 +11,6 @@ PadList::PadList(int vars_cnt, PadList *next) : refcnt(0), next_(next) {
// printf("VARS: %d\n", vars_cnt);
for (int i=0; i<vars_cnt; i++) {
SharedPtr<Value> v = UndefValue::instance();
- v->retain();
pad_.push_back(v);
}
}
@@ -29,10 +28,10 @@ void PadList::dump(VM *vm) {
void PadList::set(int index, const SharedPtr<Value> & val) {
assert(val.get());
- val->retain();
if (pad_.size() <= index) {
pad_.resize(index+1);
}
+ // val->retain();
pad_.set(index, val);
assert(pad_.at(index)->value_type == val->value_type);
}
View
@@ -12,7 +12,7 @@ void tora::peek(VM *vm, const SharedPtr<Value>& v) {
void tora::peek(VM *vm, const Value * v) {
if (v) {
- printf("Value: %s(%P)\n", v->type_str(), v);
+ printf("Value: %s(%p)\n", v->type_str(), v);
printf(" CODE SIZE: %zd\n", sizeof(CodeValue));
printf(" STR SIZE: %zd\n", sizeof(StrValue));
printf(" VALUE SIZE: %zd\n", sizeof(Value));
View
@@ -5,7 +5,6 @@
using namespace tora;
CodeValue::~CodeValue() {
-// printf("REMOVED CODEVALUE\n");
/*
if (code_params_) {
auto iter = code_params_->begin();
@@ -22,9 +21,10 @@ CodeValue::~CodeValue() {
callback_ = NULL;
}
+ /*
delete closure_var_names_;
closure_var_names_ = NULL;
- /*
+
if (closure_vars_) {
delete closure_vars_;
closure_vars_ = NULL;
View
@@ -29,7 +29,7 @@ class CodeValue: public Value {
// std::string code_name_;
boost::shared_ptr<std::vector<std::string>> code_params_;
boost::shared_ptr<std::vector<int>> code_defaults_;
- std::vector<std::string> *closure_var_names_;
+ boost::shared_ptr<std::vector<std::string>> closure_var_names_;
// std::vector<SharedPtr<Value>> *closure_vars_;
SharedPtr<PadList> pad_list_;
SharedPtr<OPArray> code_opcodes_;
@@ -48,7 +48,7 @@ class CodeValue: public Value {
, package_id_(package_id)
, func_name_id_(func_name_id)
, code_params_(code_params)
- , closure_var_names_(NULL)
+ // , closure_var_names_(NULL)
, filename_(filename)
, lineno_(lineno)
{
@@ -62,7 +62,7 @@ class CodeValue: public Value {
, package_id_(package_id)
, func_name_id_(func_name_id)
// , code_params_(NULL)
- , closure_var_names_(NULL)
+ // , closure_var_names_(NULL)
, lineno_(-1) {
}
~CodeValue();
@@ -79,10 +79,10 @@ class CodeValue: public Value {
const SharedPtr<OPArray>& code_opcodes() const {
return code_opcodes_;
}
- void closure_var_names(std::vector<std::string> *closure_var_names_) {
- closure_var_names_ = closure_var_names_;
+ void closure_var_names(const boost::shared_ptr<std::vector<std::string>> &closure_var_names__) {
+ closure_var_names_ = closure_var_names__;
}
- std::vector<std::string>*closure_var_names() const {
+ boost::shared_ptr<std::vector<std::string>> closure_var_names() const {
return closure_var_names_;
}
void code_defaults(const boost::shared_ptr<std::vector<int>> &c) {

0 comments on commit 76fad91

Please sign in to comment.