Skip to content

Commit

Permalink
Add missing delete to free up memory (rogchap#9)
Browse files Browse the repository at this point in the history
* add delete to free up memory

* don't use sprintf and free strings

* forgot to defer the freeing of memory
  • Loading branch information
rogchap authored Sep 17, 2019
1 parent 9b8bf35 commit 0b5c09a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
3 changes: 3 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,8 @@ func getError(rtn C.RtnValue) error {
Location: C.GoString(rtn.error.location),
StackTrace: C.GoString(rtn.error.stack),
}
C.free(unsafe.Pointer(rtn.error.msg))
C.free(unsafe.Pointer(rtn.error.location))
C.free(unsafe.Pointer(rtn.error.stack))
return err
}
27 changes: 11 additions & 16 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,17 @@ typedef struct {
m_ctx* ctx_ptr;
} m_value;

const char* CString(String::Utf8Value& value) {
if (value.length() == 0) {
return "empty";
}
return *value;
}

const char* CopyString(std::string str) {
char* data = static_cast<char*>(malloc(str.length()));
sprintf(data, "%s", str.c_str());
return data;
int len = str.length();
char *mem = (char*)malloc(len+1);
memcpy(mem, str.data(), len);
mem[len] = 0;
return mem;
}

const char* CopyString(String::Utf8Value& value) {
if (value.length() == 0) {
return "";
return nullptr;
}
return CopyString(*value);
}
Expand All @@ -52,7 +47,7 @@ RtnError ExceptionError(TryCatch& try_catch, Isolate* iso, Local<Context> ctx) {
RtnError rtn = {nullptr, nullptr, nullptr};

if (try_catch.HasTerminated()) {
rtn.msg = "ExecutionTerminated: script execution has been terminated";
rtn.msg = CopyString("ExecutionTerminated: script execution has been terminated");
return rtn;
}

Expand Down Expand Up @@ -175,6 +170,7 @@ void ContextDispose(ContextPtr ptr) {
Isolate::Scope isolate_scope(iso);

ctx->ptr.Reset();
delete ctx;
}

/********** Value **********/
Expand All @@ -194,6 +190,7 @@ void ValueDispose(ValuePtr ptr) {
Isolate::Scope isolate_scope(iso);

val->ptr.Reset();
delete val;
}

const char* ValueToString(ValuePtr ptr) {
Expand All @@ -209,10 +206,8 @@ const char* ValueToString(ValuePtr ptr) {
Local<Value> value = val->ptr.Get(iso);
String::Utf8Value utf8(iso, value);

char* data = static_cast<char*>(malloc(utf8.length()));
sprintf(data, "%s", *utf8);
return data;
}
return CopyString(utf8);
}


/********** Version **********/
Expand Down
10 changes: 8 additions & 2 deletions value.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package v8go

// #include <stdlib.h>
// #include "v8go.h"
import "C"
import "runtime"
import (
"runtime"
"unsafe"
)

// Value represents all Javascript values and objects
type Value struct {
Expand All @@ -13,7 +17,9 @@ type Value struct {
// are returned as-is, objects will return `[object Object]` and functions will
// print their definition.
func (v *Value) String() string {
return C.GoString(C.ValueToString(v.ptr))
s := C.ValueToString(v.ptr)
defer C.free(unsafe.Pointer(s))
return C.GoString(s)
}

func (v *Value) finalizer() {
Expand Down

0 comments on commit 0b5c09a

Please sign in to comment.