emscripten-core / emscripten-core/emscripten

Question about life cycle of emscripten::val returned from temporary vector

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

Description

In C++, one can return temporary std::vector's like this:
```
std::vector test() {
std::vector output = { 1, 2, 3 };
return output;
}
```
and not have to worry about temporary vector being destructed afterwards, thanks to RVO or move semantics.

If I were to return emscripten::val in a similar manner, and coerce it into a TypedArray:
```
emscripten::val test() {
std::vector output = { 1, 2, 3 };
return emscripten::val(typded_memory_view(output.size(), output.data()));
}
```
Can I still rest assured that the TypedArray I receive on JS won't be corrupted once the temp vector is destroyed?
Is this handled in a similar manner (i.e. using RVO or move semantics) with emscripten::val?

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.