emscripten-core / emscripten-core/emscripten

Detect leaks in HandleAllocator object when ASSERTIONS are enabled.

Open
#18,663 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Seems like it could be good to detect such leak so we could warn users if they, for example, forget to call emscripten_promise_destroy. Or do we want to just a silently ignore this?

I took a stab at it but ran into the issue that `HandleAllocator.allocated.length` doesn't change when things get deallocated.

```
diff --git a/src/library_promise.js b/src/library_promise.js
index 8de8ad9db..bf08f3ea0 100644
--- a/src/library_promise.js
+++ b/src/library_promise.js
@@ -15,6 +15,18 @@ mergeInto(LibraryManager.library, {
emscripten_promise_create__deps: ['$promiseMap'],
emscripten_promise_create__sig: 'p',
emscripten_promise_create: function() {
+#if ASSERTIONS
+ if (!promiseMap.atexit) {
+ promiseMap.atexit = () => {
+ if (promiseMap.allocated.length != 0) {
+ err('Leaked promises: ' + promiseMap.allocated.length);
+ err(promiseMap.allocated.keys().next().value);
+ abort('leaked promise handles');
+ }
+ };
+ __ATEXIT__.push(promiseMap.atexit);
+ }
+#endif
var promiseInfo = {};
promiseInfo.promise = new Promise((resolve, reject) => {
promiseInfo.reject = reject;
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.