我第一次使用Rust生成WASM时出现了以下错误,我不知道该如何进行调试。
wasm-000650c2-23:340 Uncaught RuntimeError: memory access out of bounds
at dlmalloc::dlmalloc::Dlmalloc::free::h36961b6fbcc40c05 (wasm-function[23]:670)
at __rdl_dealloc (wasm-function[367]:8)
at __rust_dealloc (wasm-function[360]:7)
at alloc::alloc::dealloc::h90df92e1f727e726 (wasm-function[146]:100)
at <alloc::alloc::Global as core::alloc::Alloc>::dealloc::h7f22ab187c7f5835 (wasm-function[194]:84)
at <alloc::raw_vec::RawVec<T, A>>::dealloc_buffer::hdce29184552be976 (wasm-function[82]:231)
at <alloc::raw_vec::RawVec<T, A> as core::ops::drop::Drop>::drop::h3910dccc175e44e6 (wasm-function[269]:38)
at core::ptr::real_drop_in_place::hd26be2408c00ce9d (wasm-function[267]:38)
at core::ptr::real_drop_in_place::h6acb013dbd13c114 (wasm-function[241]:50)
at core::ptr::real_drop_in_place::hb270ba635548ab74 (wasm-function[69]:192)
上下文:最新版本的Chrome浏览器,Rust wasm-bindgen代码被调用并从TypeScript自定义元素操作在shadow DOM中的canvas上。渲染到画布的数据来自HTML5 AudioBuffer。所有的Rust变量都是限定作用域的。
如果在文档中只有一个实例,Web组件可以完美地工作。但是,如果有多个实例,就会像上面那样转储堆栈跟踪信息。代码运行没有其他问题。
我知道Chrome浏览器中存在未解决的内存错误 - 这是这类错误的表现吗?或者有经验的Rust/wasm开发人员能否告诉我这是否不寻常?
js-sys = "0.3.19"
wasm-bindgen = "0.2.42"
wee_alloc = { version = "0.4.2", optional = true }
[dependencies.web-sys]
version = "0.3.4"
这段 Rust 代码很简短,只是将音频缓冲区的两个通道呈现到提供的 HTMLCanvasElement 中:
#[wasm_bindgen]
pub fn render(
canvas: web_sys::HtmlCanvasElement,
audio_buffer: &web_sys::AudioBuffer,
stroke_style: &JsValue,
line_width: f64,
step_size: usize,
) {
// ...
let mut channel_data: [Vec<f32>; 2] = unsafe { std::mem::uninitialized() }; // !
for channel_number in 0..1 {
channel_data[channel_number] = audio_buffer
.get_channel_data(channel_number as u32)
.unwrap();
}
// ...
我尝试注释掉功能,如果代码不涉及画布却做了上述操作,就会出现错误。作出以下更改会导致一个简单的“wam内存不足”错误。音频文件大小为1,200 k。
let channel_data: [Vec<f32>; 2] = [
audio_buffer.get_channel_data(0).unwrap(),
audio_buffer.get_channel_data(1).unwrap()
];
编辑:对于上面正确的代码,后面的内存不足
错误真的让我很困扰,但实际上这是一个Chrome bug。