emscripten-core / emscripten-core/emscripten

embind vector get() function not returning same values as block access with ptr directly from heap

Open
#14,359 0 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

Javascript function looks like this:
```
const frame = instance.RetrieveFrame();

const newArr = new Uint8Array(frame.buffer.size());
for(var i = 0; i < frame.buffer.size(); ++i)
{
newArr[i] = frame.buffer.get(i);
}
console.log(newArr);

const data = new Uint8Array(HEAPU8.buffer, frame.buffer.$$.ptr, frame.buffer.size());
console.log(data);
frame.buffer.delete();
frame.buffer = data;
```

I may just be expecting the wrong thing but `frame` variable is a struct and `frame.buffer` is a registered vector of unsigned char. I was assuming that creating a new uint8 array from the vector.$$.ptr and it's size would be the fastest accessor but it is giving me different data from iterating over the get function element by element. When I iterate each element I get the full expected frame but not when accessing the heap. I can show more code if needed but I was trying not to be overly verbose to start with and maybe someone would look at this and immediately recognize that I'm using it wrong or that my expectation for

` const data = new Uint8Array(HEAPU8.buffer, frame.buffer.$$.ptr, frame.buffer.size());`

is off base. Thanks in advance if anyone gets around to responding.

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.