| 1 |
# Support for Weak References |
| 2 |
|
| 3 |
By default wasm-bindgen does use the [TC39 weak references |
| 4 |
proposal](https://github.com/tc39/proposal-weakrefs) if support is detected. |
| 5 |
At the time of this writing all major browsers do support it. |
| 6 |
|
| 7 |
Without weak references your JS integration may be susceptible to memory leaks |
| 8 |
in Rust, for example: |
| 9 |
|
| 10 |
* You could forget to call `.free()` on a JS object, leaving the Rust memory |
| 11 |
allocated. |
| 12 |
* Rust closures converted to JS values (the `Closure` type) may not be executed |
| 13 |
and cleaned up. |
| 14 |
* Rust closures have `Closure::{into_js_value,forget}` methods which explicitly |
| 15 |
do not free the underlying memory. |
| 16 |
|
| 17 |
These issues are all solved with the weak references proposal in JS. |
| 18 |
`FinalizationRegistry` will ensure that all memory is cleaned up, regardless of |
| 19 |
whether it's explicitly deallocated or not. Note that explicit deallocation |
| 20 |
is always a possibility and supported, but if it's not called then memory will |
| 21 |
still be automatically deallocated if `FinalizationRegistry` support is detected. |