fix(napi): arraybuffer memory leak (#1420)

This commit is contained in:
overlookmotel 2023-01-09 06:47:36 +00:00 committed by GitHub
parent 44eb4c6341
commit ce2a29fca7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 0 deletions

View file

@ -125,6 +125,7 @@ macro_rules! impl_typed_array {
fn noop_finalize(_data: *mut $rust_type, _length: usize) {} fn noop_finalize(_data: *mut $rust_type, _length: usize) {}
pub fn new(mut data: Vec<$rust_type>) -> Self { pub fn new(mut data: Vec<$rust_type>) -> Self {
data.shrink_to_fit();
let ret = $name { let ret = $name {
data: data.as_mut_ptr(), data: data.as_mut_ptr(),
length: data.len(), length: data.len(),

View file

@ -17,6 +17,7 @@ while (true) {
api.arrayBufferLen() api.arrayBufferLen()
api.bufferConvert(Buffer.from(FIXTURE)) api.bufferConvert(Buffer.from(FIXTURE))
api.arrayBufferConvert(Uint8Array.from(FIXTURE)) api.arrayBufferConvert(Uint8Array.from(FIXTURE))
api.arrayBufferCreateFromVecWithSpareCapacity()
api.bufferPassThrough(Buffer.from(FIXTURE)) api.bufferPassThrough(Buffer.from(FIXTURE))
api.arrayBufferPassThrough(Uint8Array.from(FIXTURE)) api.arrayBufferPassThrough(Uint8Array.from(FIXTURE))
if (i % 10 === 0) { if (i % 10 === 0) {

View file

@ -157,6 +157,13 @@ pub fn array_buffer_convert(array_buffer: Uint8Array) -> Uint8Array {
Uint8Array::new(vec![1; array_buffer.len()]) Uint8Array::new(vec![1; array_buffer.len()])
} }
#[napi]
pub fn array_buffer_create_from_vec_with_spare_capacity() -> Uint8Array {
let mut v = vec![1; 1024 * 10240];
v.truncate(1);
Uint8Array::new(v)
}
#[napi] #[napi]
pub fn array_buffer_len() -> u32 { pub fn array_buffer_len() -> u32 {
Uint8Array::new(vec![1; 1024 * 10240]).len() as u32 Uint8Array::new(vec![1; 1024 * 10240]).len() as u32