Skip to content

Fix memory leaks detected by running tests with AddressSanitizer #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/AdblockPlus/JsEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ namespace AdblockPlus

JsValue GetGlobalObject();

JsEnginePtr GetSharedPtr()
{
return shared_from_this();
}

Platform& platform;
/// Isolate must be disposed only after disposing of all objects which are
/// using it.
Expand Down
20 changes: 8 additions & 12 deletions src/JsEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ namespace
ScopedV8Isolate()
{
V8Initializer::Init();
allocator.reset(v8::ArrayBuffer::Allocator::NewDefaultAllocator());
v8::Isolate::CreateParams isolateParams;
isolateParams.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
isolateParams.array_buffer_allocator = allocator.get();
isolate = v8::Isolate::New(isolateParams);
}

Expand All @@ -101,6 +102,7 @@ namespace
ScopedV8Isolate(const ScopedV8Isolate&);
ScopedV8Isolate& operator=(const ScopedV8Isolate&);

std::unique_ptr<v8::ArrayBuffer::Allocator> allocator;
v8::Isolate* isolate;
};
}
Expand Down Expand Up @@ -268,12 +270,10 @@ AdblockPlus::JsValue AdblockPlus::JsEngine::NewCallback(
{
const JsContext context(*this);
auto isolate = GetIsolate();
// Note: we are leaking this weak pointer, no obvious way to destroy it when
// it's no longer used
std::weak_ptr<JsEngine>* data =
new std::weak_ptr<JsEngine>(shared_from_this());
// The callback may not outlive us since it lives out of our isolate.
// It's safe to bind a bare pointer to self.
v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate, callback,
v8::External::New(isolate, data));
v8::External::New(isolate, this));
return JsValue(shared_from_this(),
CHECKED_TO_LOCAL(isolate, templ->GetFunction(isolate->GetCurrentContext())));
}
Expand All @@ -282,12 +282,8 @@ AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Function
{
const v8::Local<const v8::External> external =
v8::Local<const v8::External>::Cast(arguments.Data());
std::weak_ptr<JsEngine>* data =
static_cast<std::weak_ptr<JsEngine>*>(external->Value());
JsEnginePtr result = data->lock();
if (!result)
throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?");
return result;
JsEngine* engine = static_cast<JsEngine*>(external->Value());
return engine->GetSharedPtr();
}

JsEngine::JsWeakValuesID JsEngine::StoreJsValues(const JsValueList& values)
Expand Down