为什么 Emscripten 会为添加到分配的 Float32Array 中的某些数字抛出错误?
Why does Emscripten throw errors for some number added to an allocated Float32Array?
更新:
↓ This has been resolved↓,我自己的回答没法马上采纳
我正在学习如何在使用 Emscripten 编译的 JavaScript 和 "C++" 之间传递 TypeArray
s。在这个过程中我 运行 犯了一个错误我不知道该怎么做。
这基本上就是我正在做的事情:
let bufferLength = 32
let ptr = Module._malloc(bufferLength)
let buf = new Float32Array(Module.HEAPF32.buffer, ptr, bufferLength)
for (let i = 0; i < bufferLength; i++) {
// The multiplier (here 1.234) sometimes causes an error,
// and sometimes it doesn't
buf[i] = i * 1.234
}
// Here is where the error is thrown and Module._abort()
// is invoked
Module._free(ptr)
我已经建立了一个可重现的仓库:https://github.com/alexanderwallin/emcc-f32-buffer-error。回购测试乘数 [1.1, 1.2, 1.3, 1.4]
,其中 1.1
和 1.4
导致错误。
Module
在这种情况下是使用 emcc -o empty.js empty.c
.
编译的空 C 文件
这是控制台输出:
- - - - - - -
f32-ptr-tests.js:24 testing multiplier: 1.1
f32-ptr-tests.js:34 ... did fill array
f32-ptr-tests.js:41 oh nose!
f32-ptr-tests.js:42 abort() at Error
at jsStackTrace (file:///path/to/emcc-f32-ptr-error/empty.js:1173:13)
at stackTrace (file:///path/to/emcc-f32-ptr-error/empty.js:1190:22)
at Object.abort (file:///path/to/emcc-f32-ptr-error/empty.js:9814:44)
at _abort (file:///path/to/emcc-f32-ptr-error/empty.js:1796:22)
at _free (file:///path/to/emcc-f32-ptr-error/empty.js:8998:3)
at Object.asm._free (file:///path/to/emcc-f32-ptr-error/empty.js:9602:19)
at file:///path/to/emcc-f32-ptr-error/f32-ptr-tests.js:36:12
(anonymous) @ f32-ptr-tests.js:42
f32-ptr-tests.js:45
f32-ptr-tests.js:23 - - - - - - -
f32-ptr-tests.js:24 testing multiplier: 1.2
f32-ptr-tests.js:34 ... did fill array
f32-ptr-tests.js:38 all good
f32-ptr-tests.js:45
f32-ptr-tests.js:23 - - - - - - -
f32-ptr-tests.js:24 testing multiplier: 1.3
f32-ptr-tests.js:34 ... did fill array
f32-ptr-tests.js:38 all good
f32-ptr-tests.js:45
f32-ptr-tests.js:23 - - - - - - -
f32-ptr-tests.js:24 testing multiplier: 1.4
f32-ptr-tests.js:34 ... did fill array
f32-ptr-tests.js:41 oh nose!
f32-ptr-tests.js:42 abort() at Error
at jsStackTrace (file:///path/to/emcc-f32-ptr-error/empty.js:1173:13)
at stackTrace (file:///path/to/emcc-f32-ptr-error/empty.js:1190:22)
at Object.abort (file:///path/to/emcc-f32-ptr-error/empty.js:9814:44)
at _abort (file:///path/to/emcc-f32-ptr-error/empty.js:1796:22)
at _free (file:///path/to/emcc-f32-ptr-error/empty.js:8998:3)
at Object.asm._free (file:///path/to/emcc-f32-ptr-error/empty.js:9602:19)
at file:///path/to/emcc-f32-ptr-error/f32-ptr-tests.js:36:12
这里发生了什么?
这是一个很好的 101 内存管理课。
我正在分配 32 个字节,但由于 Float32Array
包含 32 位(4 字节)数字,我只分配了我需要的 25% 的内存。这意味着当我将数字写入数组的最后 3 个四分之一时,我会覆盖应用程序中其他地方的内存,这有时会导致错误,有时不会。
所以,改变
let ptr = Module._malloc(bufferLength)
到
let ptr = Module._malloc(bufferLength * Float32Array.BYTES_PER_ELEMENT)
成功了。
更新:
↓ This has been resolved↓,我自己的回答没法马上采纳
我正在学习如何在使用 Emscripten 编译的 JavaScript 和 "C++" 之间传递 TypeArray
s。在这个过程中我 运行 犯了一个错误我不知道该怎么做。
这基本上就是我正在做的事情:
let bufferLength = 32
let ptr = Module._malloc(bufferLength)
let buf = new Float32Array(Module.HEAPF32.buffer, ptr, bufferLength)
for (let i = 0; i < bufferLength; i++) {
// The multiplier (here 1.234) sometimes causes an error,
// and sometimes it doesn't
buf[i] = i * 1.234
}
// Here is where the error is thrown and Module._abort()
// is invoked
Module._free(ptr)
我已经建立了一个可重现的仓库:https://github.com/alexanderwallin/emcc-f32-buffer-error。回购测试乘数 [1.1, 1.2, 1.3, 1.4]
,其中 1.1
和 1.4
导致错误。
Module
在这种情况下是使用 emcc -o empty.js empty.c
.
这是控制台输出:
- - - - - - -
f32-ptr-tests.js:24 testing multiplier: 1.1
f32-ptr-tests.js:34 ... did fill array
f32-ptr-tests.js:41 oh nose!
f32-ptr-tests.js:42 abort() at Error
at jsStackTrace (file:///path/to/emcc-f32-ptr-error/empty.js:1173:13)
at stackTrace (file:///path/to/emcc-f32-ptr-error/empty.js:1190:22)
at Object.abort (file:///path/to/emcc-f32-ptr-error/empty.js:9814:44)
at _abort (file:///path/to/emcc-f32-ptr-error/empty.js:1796:22)
at _free (file:///path/to/emcc-f32-ptr-error/empty.js:8998:3)
at Object.asm._free (file:///path/to/emcc-f32-ptr-error/empty.js:9602:19)
at file:///path/to/emcc-f32-ptr-error/f32-ptr-tests.js:36:12
(anonymous) @ f32-ptr-tests.js:42
f32-ptr-tests.js:45
f32-ptr-tests.js:23 - - - - - - -
f32-ptr-tests.js:24 testing multiplier: 1.2
f32-ptr-tests.js:34 ... did fill array
f32-ptr-tests.js:38 all good
f32-ptr-tests.js:45
f32-ptr-tests.js:23 - - - - - - -
f32-ptr-tests.js:24 testing multiplier: 1.3
f32-ptr-tests.js:34 ... did fill array
f32-ptr-tests.js:38 all good
f32-ptr-tests.js:45
f32-ptr-tests.js:23 - - - - - - -
f32-ptr-tests.js:24 testing multiplier: 1.4
f32-ptr-tests.js:34 ... did fill array
f32-ptr-tests.js:41 oh nose!
f32-ptr-tests.js:42 abort() at Error
at jsStackTrace (file:///path/to/emcc-f32-ptr-error/empty.js:1173:13)
at stackTrace (file:///path/to/emcc-f32-ptr-error/empty.js:1190:22)
at Object.abort (file:///path/to/emcc-f32-ptr-error/empty.js:9814:44)
at _abort (file:///path/to/emcc-f32-ptr-error/empty.js:1796:22)
at _free (file:///path/to/emcc-f32-ptr-error/empty.js:8998:3)
at Object.asm._free (file:///path/to/emcc-f32-ptr-error/empty.js:9602:19)
at file:///path/to/emcc-f32-ptr-error/f32-ptr-tests.js:36:12
这里发生了什么?
这是一个很好的 101 内存管理课。
我正在分配 32 个字节,但由于 Float32Array
包含 32 位(4 字节)数字,我只分配了我需要的 25% 的内存。这意味着当我将数字写入数组的最后 3 个四分之一时,我会覆盖应用程序中其他地方的内存,这有时会导致错误,有时不会。
所以,改变
let ptr = Module._malloc(bufferLength)
到
let ptr = Module._malloc(bufferLength * Float32Array.BYTES_PER_ELEMENT)
成功了。