-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Question about WeakRef basics #2201
Comments
There's a number of ways to manage memory here. For example Until the |
What about the case where the closure is never actually invoked (such as my example when
Does today's WASM_BINDGEN_WEAKREF-gated feature aleady support this (in any browser)? If so, is there a code example showing how it's meant to work? I wasn't able to find one. The thing I got hung up on is that we have Untested code, may not compile, but hopefully this shows what I mean: #[wasm_bindgen]
extern {
#[wasm_bindgen]
fn mushyJSFunction(effect: &Closure<dyn Fn()>);
}
fn foo() {
let closure = Closure::new(|| {});
mushyJSFunction(&closure);
// Here, `closure` is dropped and can no longer be called.
// How do you persist it without `Box::leak`?
} |
Yes The current support for |
🙂 Thanks for the response! Sounds like I might be jumping the gun a bit. I'll wait until things have stabilized a little more |
FWIW I updated this in #2248 and I think that closures should be handled now. Closures are still explicitly deallocated on |
Let's say I have a JS function, not under my control. It takes a callback, and maybe calls it sometime in the future. Example:
As I understand it, using stable features today, calling this function from Rust with a
Closure
necessitates a memory leak on the Rust side. This is because you need to keep the Closure alive for it to be called, and there's no way to know when JS is finished with the closure.Does the new
WeakRef
functionality let you fix that memory leak? (of course subject to GC nondeterminism)Or to rephrase, can you pass "ownership" of a Rust value to the JS garbage collector, including eventually being dropped/deallocated properly across the FFI barrier?
The text was updated successfully, but these errors were encountered: