cvm-rs: don't leave reference to input buffer live

i think this was causing it to not be freed because we were holding a live reference (to NAPI/node/v8 side of things) even after we were done so the input buffer just never got freed from the buffer pool
This commit is contained in:
modeco80 2024-08-23 04:39:23 -04:00
parent 17191b0917
commit 7d9fab2485

View file

@ -46,8 +46,8 @@ pub struct JpegInputArgs {
#[napi(js_name = "jpegEncode")] #[napi(js_name = "jpegEncode")]
#[allow(unused)] #[allow(unused)]
pub fn jpeg_encode(env: Env, input: JpegInputArgs) -> napi::Result<napi::JsObject> { pub fn jpeg_encode(env: Env, input: JpegInputArgs) -> napi::Result<napi::JsObject> {
let (deferred_resolver, promise) = env.create_deferred::<napi::JsUnknown, _>()?; let (deferred_resolver, promise) = env.create_deferred::<napi::JsBuffer, _>()?;
let buf = input.buffer.into_ref()?; let mut buf = input.buffer.into_ref()?;
// Spawn a task on the rayon pool that encodes the JPEG and fufills the promise // Spawn a task on the rayon pool that encodes the JPEG and fufills the promise
// once it is done encoding. // once it is done encoding.
@ -72,7 +72,9 @@ pub fn jpeg_encode(env: Env, input: JpegInputArgs) -> napi::Result<napi::JsObjec
let buffer = env let buffer = env
.create_buffer_with_data(vec) .create_buffer_with_data(vec)
.expect("Couldn't create node Buffer, things are probably very broken by this point"); .expect("Couldn't create node Buffer, things are probably very broken by this point");
Ok(buffer.into_unknown()) // no longer need the input buffer
buf.unref(env)?;
Ok(buffer.into_raw())
}); });
}); });