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

Memory management #2

Open
mmarczell-graphisoft opened this issue Dec 21, 2021 · 1 comment
Open

Memory management #2

mmarczell-graphisoft opened this issue Dec 21, 2021 · 1 comment

Comments

@mmarczell-graphisoft
Copy link

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.

@daminetreg
Copy link
Owner

daminetreg commented Dec 22, 2021

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants