js-bind icon indicating copy to clipboard operation
js-bind copied to clipboard

Memory management

Open mmarczell-graphisoft opened this issue 3 years ago • 1 comments

What is the memory management approach of this library? Are the C++ callbacks freed? Can I free them explicitly? This is important because they might be holding onto e. g. heavyweight objects through capture.

mmarczell-graphisoft avatar Dec 21 '21 13:12 mmarczell-graphisoft

It's based on embind emscripten::val, hence if you stop reffering it from JS or from C++ it gets destroyed :

clickme_btn.set("onclick", js::bind(onclick, _1));

js::bind creates an std::function object managed by an emscripten::val referring to the onclick function/lambda. emscripten::val is like a shared_ptr between 2 worlds C++ and JS. It has a reference count and if nobody uses it anymore it gets deleted.

In this case if I change the "onclick" event handler of clickme_btn to something else the previous std::function should get deleted (as soon as JS garbage collection kicks in, as the reference is kept by an object in the JS space).

daminetreg avatar Dec 22 '21 09:12 daminetreg