Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ArrayBuffer memory leak #1420

Merged
merged 1 commit into from Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs
Expand Up @@ -125,6 +125,7 @@ macro_rules! impl_typed_array {
fn noop_finalize(_data: *mut $rust_type, _length: usize) {}

pub fn new(mut data: Vec<$rust_type>) -> Self {
data.shrink_to_fit();
let ret = $name {
data: data.as_mut_ptr(),
length: data.len(),
Expand Down
1 change: 1 addition & 0 deletions memory-testing/buffer.mjs
Expand Up @@ -17,6 +17,7 @@ while (true) {
api.arrayBufferLen()
api.bufferConvert(Buffer.from(FIXTURE))
api.arrayBufferConvert(Uint8Array.from(FIXTURE))
api.arrayBufferCreateFromVecWithSpareCapacity()
api.bufferPassThrough(Buffer.from(FIXTURE))
api.arrayBufferPassThrough(Uint8Array.from(FIXTURE))
if (i % 10 === 0) {
Expand Down
7 changes: 7 additions & 0 deletions memory-testing/src/lib.rs
Expand Up @@ -157,6 +157,13 @@ pub fn array_buffer_convert(array_buffer: Uint8Array) -> Uint8Array {
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]
pub fn array_buffer_len() -> u32 {
Uint8Array::new(vec![1; 1024 * 10240]).len() as u32
Expand Down