Skip to content
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

Fix v8::OwnedIsolate memory leak caused by retain cycle in MiniV8::create_function #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 7 additions & 5 deletions src/mini_v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl MiniV8 {

self.scope(|scope| {
let callback = Box::new(func);
let callback_info = CallbackInfo { mv8: self.clone(), callback };
let callback_info = CallbackInfo { callback };
let ptr = Box::into_raw(Box::new(callback_info));
let ext = v8::External::new(scope, ptr as _);

Expand All @@ -199,12 +199,12 @@ impl MiniV8 {
let ext = v8::Local::<v8::External>::try_from(data).unwrap();
let callback_info_ptr = ext.value() as *mut CallbackInfo;
let callback_info = unsafe { &mut *callback_info_ptr };
let CallbackInfo { mv8, callback } = callback_info;
let CallbackInfo { callback } = callback_info;
let ptr = scope as *mut v8::HandleScope;
// We can erase the lifetime of the `v8::HandleScope` safely because it only lives
// on the interface stack during the current block:
let ptr: *mut v8::HandleScope<'static> = unsafe { std::mem::transmute(ptr) };
mv8.interface.push(ptr);
let mv8 = MiniV8 { interface: Interface::new_scopped(ptr) };
let this = Value::from_v8_value(&mv8, scope, fca.this().into());
let len = fca.length();
let mut args = Vec::with_capacity(len as usize);
Expand All @@ -220,7 +220,6 @@ impl MiniV8 {
scope.throw_exception(exception);
},
};
mv8.interface.pop();
};

let value = v8::Function::builder(v8_func).data(ext.into()).build(scope).unwrap();
Expand Down Expand Up @@ -317,6 +316,10 @@ impl Interface {
Interface(Rc::new(RefCell::new(vec![Rc::new(RefCell::new(InterfaceEntry::Isolate(isolate)))])))
}

fn new_scopped(handle_scope:*mut v8::HandleScope<'static>) -> Interface {
Interface(Rc::new(RefCell::new(vec![Rc::new(RefCell::new(InterfaceEntry::HandleScope(handle_scope)))])))
}

fn push(&self, handle_scope: *mut v8::HandleScope<'static>) {
self.0.borrow_mut().push(Rc::new(RefCell::new(InterfaceEntry::HandleScope(handle_scope))));
}
Expand Down Expand Up @@ -436,7 +439,6 @@ fn add_finalizer<T: 'static>(
type Callback = Box<dyn Fn(&MiniV8, Value, Values) -> Result<Value>>;

struct CallbackInfo {
mv8: MiniV8,
callback: Callback,
}

Expand Down