Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is just calling deleteLater() enough to avoid memory leaks in Javascript with Embind?

I'm a longtime Java/C++ programmer and novice Javascript programmer. I'm trying to make a web app with a class I have previously coded in C++.

In my Javascript web app, I'm using Embind to create and use the class originally coded in C++. On the Embind documentation page it says,

JavaScript code must explicitly delete any C++ object handles it has received, or the Emscripten heap will grow indefinitely.

and the examples on the page show the created object being deleted immediately after use:

var x = new Module.MyClass; x.method(); x.delete();

In my web app, I want my object from C++ to persist for the lifetime of the webpage. I want to be able to press a button on the page and update the state of my object. If I .delete() the object at the end of the script, it won't persist when I try pushing the button later.

In the Embind example, embind.test.js, it is possible to call .deleteLater() on a newly created object:

var v = (new cm.ValHolder({})).deleteLater();

My question is, if I simply call .deleteLater() upon the object creation, is this enough for the object to be deleted when the app is done running or when the page is closed? I'm trying to avoid growing the heap indefinitely or cause any memory leaks.

Again, I'm new to Javascript so please point out if I'm missing anything obvious or if ignorant of a best practice concerning memory leaks and pointers in Javascript.

Let me know if I need to clarify anything. Thanks!

reference: https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#memory-management

like image 340
caiminatrix Avatar asked Nov 25 '25 22:11

caiminatrix


1 Answers

I'm on the same path and really don't have a concrete answer for you, but as a JS developer I can collaborate with this:

  • Calling delete() on an already deleted object throws an ugly exception (uncatchable from JS). There is an undocumented method to check this: obj.isDeleted(). Also obj.SS.count will be 0 when the object is "deleted"
  • In the browser not deleting objects could for sure break your application but from the point of view of a C++ developer this only happens in the context of the document - so by just reloading the page - you get all the memory back (is not necessary to kill the browser)
  • when exceptions are thrown from C++ code , like sigint, bad memory allocation, etc, it seems is not possible to catch them, even using DISABLE_EXCEPTION_CATCHING or the rest of the debug flags. Module.onAbort or similar also won't handle them. So If anybody will handle deleteLater() registered objects when the program throws must be at C++ side.
  • I see there's no documentation about delete(), isDeleted() deleteLater() I think those would be great candidates for a PR.
like image 163
cancerbero Avatar answered Nov 27 '25 10:11

cancerbero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!