Skip to content

Commit 6b776bb

Browse files
authored
Call NewDefaultAllocator() for v8::Isolate::CreateParams field (#198)
- fallback to own implementation for old V8 versions prior to 5.4 - store created allocator as `unique_ptr` in`v8pp::context` to avoid memory leaks
1 parent c07e195 commit 6b776bb

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

v8pp/context.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ void context::run_file(v8::FunctionCallbackInfo<v8::Value> const& args)
129129
args.GetReturnValue().Set(scope.Escape(result));
130130
}
131131

132+
#if V8_MAJOR_VERSION < 5 || (V8_MAJOR_VERSION == 5 && V8_MINOR_VERSION < 4)
132133
struct array_buffer_allocator : v8::ArrayBuffer::Allocator
133134
{
134135
void* Allocate(size_t length)
@@ -145,12 +146,16 @@ struct array_buffer_allocator : v8::ArrayBuffer::Allocator
145146
(void)length;
146147
}
147148
};
148-
static array_buffer_allocator array_buffer_allocator_;
149+
#endif
149150

150151
v8::Isolate* context::create_isolate(v8::ArrayBuffer::Allocator* allocator)
151152
{
152153
v8::Isolate::CreateParams create_params;
153-
create_params.array_buffer_allocator = allocator ? allocator : &array_buffer_allocator_;
154+
#if V8_MAJOR_VERSION < 5 || (V8_MAJOR_VERSION == 5 && V8_MINOR_VERSION < 4)
155+
create_params.array_buffer_allocator = allocator ? allocator : new array_buffer_allocator;
156+
#else
157+
create_params.array_buffer_allocator = allocator ? allocator : v8::ArrayBuffer::Allocator::NewDefaultAllocator();
158+
#endif
154159

155160
return v8::Isolate::New(create_params);
156161
}
@@ -165,6 +170,11 @@ context::context(v8::Isolate* isolate, v8::ArrayBuffer::Allocator* allocator,
165170
if (own_isolate_)
166171
{
167172
isolate_->Enter();
173+
if (!allocator)
174+
{
175+
// take ownership over the new allocator created in create_isolate()
176+
array_buffer_allocator_.reset(isolate_->GetArrayBufferAllocator());
177+
}
168178
}
169179

170180
v8::HandleScope scope(isolate_);
@@ -196,6 +206,7 @@ context::context(context&& src) noexcept
196206
, enter_context_(std::exchange(src.enter_context_, false))
197207
, isolate_(std::exchange(src.isolate_, nullptr))
198208
, impl_(std::move(src.impl_))
209+
, array_buffer_allocator_(std::move(src.array_buffer_allocator_))
199210
, modules_(std::move(src.modules_))
200211
, lib_path_(std::move(src.lib_path_))
201212
{
@@ -210,6 +221,7 @@ context& context::operator=(context&& src) noexcept
210221
own_isolate_ = std::exchange(src.own_isolate_, false);
211222
enter_context_ = std::exchange(src.enter_context_, false);
212223
isolate_ = std::exchange(src.isolate_, nullptr);
224+
array_buffer_allocator_ = std::move(src.array_buffer_allocator_);
213225
impl_ = std::move(src.impl_);
214226
modules_ = std::move(src.modules_);
215227
lib_path_ = std::move(src.lib_path_);
@@ -260,6 +272,7 @@ void context::destroy()
260272
isolate_->Dispose();
261273
}
262274
isolate_ = nullptr;
275+
array_buffer_allocator_.reset();
263276
}
264277

265278
context& context::value(std::string_view name, v8::Local<v8::Value> value)

v8pp/context.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class context
108108
bool enter_context_;
109109
v8::Isolate* isolate_;
110110
v8::Global<v8::Context> impl_;
111+
std::unique_ptr<v8::ArrayBuffer::Allocator> array_buffer_allocator_;
111112

112113
static void load_module(v8::FunctionCallbackInfo<v8::Value> const& args);
113114
static void run_file(v8::FunctionCallbackInfo<v8::Value> const& args);

0 commit comments

Comments
 (0)