提交 f25220b2 编写于 作者: R Ryan Dahl

Fix test_cc memory leaks.

These were discovered using the LSAN.
http://dev.chromium.org/developers/testing/leaksanitizer
上级 b39f4c14
...@@ -16,8 +16,7 @@ extern "C" { ...@@ -16,8 +16,7 @@ extern "C" {
Deno* deno_new(deno_buf snapshot, deno_buf shared, deno_recv_cb cb) { Deno* deno_new(deno_buf snapshot, deno_buf shared, deno_recv_cb cb) {
deno::DenoIsolate* d = new deno::DenoIsolate(snapshot, cb, shared); deno::DenoIsolate* d = new deno::DenoIsolate(snapshot, cb, shared);
v8::Isolate::CreateParams params; v8::Isolate::CreateParams params;
params.array_buffer_allocator = params.array_buffer_allocator = d->array_buffer_allocator_;
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
params.external_references = deno::external_references; params.external_references = deno::external_references;
if (snapshot.data_ptr) { if (snapshot.data_ptr) {
...@@ -55,8 +54,9 @@ Deno* deno_new_snapshotter(deno_buf shared, deno_recv_cb cb, ...@@ -55,8 +54,9 @@ Deno* deno_new_snapshotter(deno_buf shared, deno_recv_cb cb,
auto* d = new deno::DenoIsolate(deno::empty_buf, cb, shared); auto* d = new deno::DenoIsolate(deno::empty_buf, cb, shared);
d->snapshot_creator_ = creator; d->snapshot_creator_ = creator;
d->AddIsolate(isolate); d->AddIsolate(isolate);
v8::Isolate::Scope isolate_scope(isolate);
{ {
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate); v8::HandleScope handle_scope(isolate);
auto context = v8::Context::New(isolate); auto context = v8::Context::New(isolate);
creator->SetDefaultContext(context, creator->SetDefaultContext(context,
...@@ -186,7 +186,6 @@ void deno_check_promise_errors(Deno* d_) { ...@@ -186,7 +186,6 @@ void deno_check_promise_errors(Deno* d_) {
void deno_delete(Deno* d_) { void deno_delete(Deno* d_) {
deno::DenoIsolate* d = reinterpret_cast<deno::DenoIsolate*>(d_); deno::DenoIsolate* d = reinterpret_cast<deno::DenoIsolate*>(d_);
d->isolate_->Dispose();
delete d; delete d;
} }
......
...@@ -34,6 +34,8 @@ Deno* deno_new(deno_buf snapshot, deno_buf shared, deno_recv_cb cb); ...@@ -34,6 +34,8 @@ Deno* deno_new(deno_buf snapshot, deno_buf shared, deno_recv_cb cb);
Deno* deno_new_snapshotter(deno_buf shared, deno_recv_cb cb, Deno* deno_new_snapshotter(deno_buf shared, deno_recv_cb cb,
const char* js_filename, const char* js_source, const char* js_filename, const char* js_source,
const char* source_map); const char* source_map);
// Generate a snapshot. The resulting buf can be used with deno_new.
// The caller must free the returned data by calling delete[] buf.data_ptr.
deno_buf deno_get_snapshot(Deno* d); deno_buf deno_get_snapshot(Deno* d);
void deno_delete(Deno* d); void deno_delete(Deno* d);
......
...@@ -23,15 +23,26 @@ class DenoIsolate { ...@@ -23,15 +23,26 @@ class DenoIsolate {
cb_(cb), cb_(cb),
next_req_id_(0), next_req_id_(0),
user_data_(nullptr) { user_data_(nullptr) {
array_buffer_allocator_ = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
if (snapshot.data_ptr) { if (snapshot.data_ptr) {
snapshot_.data = reinterpret_cast<const char*>(snapshot.data_ptr); snapshot_.data = reinterpret_cast<const char*>(snapshot.data_ptr);
snapshot_.raw_size = static_cast<int>(snapshot.data_len); snapshot_.raw_size = static_cast<int>(snapshot.data_len);
} }
} }
~DenoIsolate() {
if (snapshot_creator_) {
delete snapshot_creator_;
} else {
isolate_->Dispose();
}
delete array_buffer_allocator_;
}
void AddIsolate(v8::Isolate* isolate); void AddIsolate(v8::Isolate* isolate);
v8::Isolate* isolate_; v8::Isolate* isolate_;
v8::ArrayBuffer::Allocator* array_buffer_allocator_;
deno_buf shared_; deno_buf shared_;
const v8::FunctionCallbackInfo<v8::Value>* current_args_; const v8::FunctionCallbackInfo<v8::Value>* current_args_;
v8::SnapshotCreator* snapshot_creator_; v8::SnapshotCreator* snapshot_creator_;
......
...@@ -14,15 +14,22 @@ TEST(LibDenoTest, InitializesCorrectlyWithoutSnapshot) { ...@@ -14,15 +14,22 @@ TEST(LibDenoTest, InitializesCorrectlyWithoutSnapshot) {
deno_delete(d); deno_delete(d);
} }
TEST(LibDenoTest, SnapshotterInitializesCorrectly) {
Deno* d = deno_new_snapshotter(empty, nullptr, "a.js", "a = 1 + 2", nullptr);
deno_delete(d);
}
TEST(LibDenoTest, Snapshotter) { TEST(LibDenoTest, Snapshotter) {
Deno* d1 = deno_new_snapshotter(empty, nullptr, "a.js", "a = 1 + 2", nullptr); Deno* d1 = deno_new_snapshotter(empty, nullptr, "a.js", "a = 1 + 2", nullptr);
deno_buf test_snapshot = deno_get_snapshot(d1); deno_buf test_snapshot = deno_get_snapshot(d1);
// TODO(ry) deno_delete(d1); deno_delete(d1);
Deno* d2 = deno_new(test_snapshot, empty, nullptr); Deno* d2 = deno_new(test_snapshot, empty, nullptr);
EXPECT_TRUE( EXPECT_TRUE(
deno_execute(d2, nullptr, "b.js", "if (a != 3) throw Error('x');")); deno_execute(d2, nullptr, "b.js", "if (a != 3) throw Error('x');"));
deno_delete(d2); deno_delete(d2);
delete[] test_snapshot.data_ptr;
} }
TEST(LibDenoTest, CanCallFunction) { TEST(LibDenoTest, CanCallFunction) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册