emscripten-core / emscripten-core/emscripten

How to new an em-instance without INST.DELETE() and without memory LEAK?

Open
#20,569 8 comments 1 reaction 0 assignees View on GitHub
embind
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

I use **embind** and **.smart_ptr_constructor()** to bind a C++ class.

I suppose a smart-pointer could help me to write javascript easy without calling .delete(), but the browser console still warning:
> xmc.js:1423 **Embind found a leaked C++ instance** Random <0x000113d8>.

Since .delete() is easily interrupted by exception or something else, how can I use emscripten really safe and concise?

My C++ header:
``` c++
#ifndef RANDOM_H
#define RANDOM_H

#include

extern "C" {
/**
* @brief Generates random number.
*/
class Random {
private:
long long seed;
public:
/**
* @brief Construct randomizer using a seed.
* WITH fixed-number to get false-random.
* WITH timestamp to get real-random.
*/
Random(double seed);
int next();
float nextFloat();
double nextDouble();
};

std::shared_ptr newRandom(double seed);
}

#endif
```

My C++ soure:
``` c++
#include "random.h"

Random::Random(double seed) {
this->seed = (long long)seed;
if (this->seed < 0) {
this->seed = -this->seed;
}
if (this->seed >= 233280) {
this->seed = this->seed % 233280;
}
}

int Random::next() {
this->seed = (this->seed * 9301 + 49297) % 233280;
return this->seed;
}

float Random::nextFloat() {
return (float) this->next() / 233280.0f;
}

double Random::nextDouble() {
return (double) this->next() / 233280.0;
}

std::shared_ptr newRandom(double seed) {
return std::make_shared(seed);
}
```

My embind wrapper:
``` c++
#include "random.h"
#include

using namespace emscripten;

EMSCRIPTEN_BINDINGS(module){
class_("Random")
.smart_ptr_constructor("Random", &std::make_shared)
.function("next", &Random::next)
.function("nextFloat", &Random::nextFloat)
.function("nextDouble", &Random::nextDouble);
function("newRandom", &newRandom);
}
```

My build command:
``` bash
em++ --bind -O0 -s SINGLE_FILE=1 -s EXPORT_NAME=xmc -I include src/random.cpp js/em_bind/xmcwrap.cpp -o js/lib/xmc.js
```

My javascript test:
``` javascript

async function waitWasm() {
return new Promise((resolve) => {
if (Module["calledRun"]) {
return resolve();
}
window.Module.onRuntimeInitialized = function() {
return resolve();
}
});
}

await waitWasm();

const rand = new Module.Random(new Date().getTime());

const r3 = [];
for (let i = 0; i < 10; i++) {
r3.push(rand.next())
}

const r4 = [];
for (let i = 0; i < 10; i++) {
r4.push(rand.nextFloat())
}

const r5 = [];
for (let i = 0; i < 10; i++) {
r5.push(rand.nextDouble())
}

console.log(r3, r4, r5);

```

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.