Skip to content

Commit

Permalink
fix: remove Ref usage to avoid memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
mixazizup committed Aug 27, 2021
1 parent 72bca65 commit ff72bdc
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ extern crate napi_derive;
use std::ffi::CString;

use napi::{
CallContext, Env, Error, JsBoolean, JsBuffer, JsBufferValue, JsObject, JsUnknown, Ref, Result,
Status, Task,
CallContext, Env, Error, JsBoolean, JsBuffer, JsObject, JsUnknown, Result, Status, Task,
};
use snap::raw::{Decoder, Encoder};

Expand All @@ -30,7 +29,7 @@ fn init(mut exports: JsObject) -> Result<()> {

struct Enc {
inner: Encoder,
data: Ref<JsBufferValue>,
data: Vec<u8>,
}

impl Task for Enc {
Expand All @@ -52,7 +51,7 @@ impl Task for Enc {

struct Dec {
inner: Decoder,
data: Ref<JsBufferValue>,
data: Vec<u8>,
as_buffer: bool,
}

Expand Down Expand Up @@ -98,7 +97,7 @@ fn compress(ctx: CallContext) -> Result<JsObject> {
let enc = Encoder::new();
let encoder = Enc {
inner: enc,
data: data.into_ref()?,
data: data.into_value()?.to_vec(),
};
ctx.env.spawn(encoder).map(|v| v.promise_object())
}
Expand Down Expand Up @@ -133,7 +132,7 @@ fn uncompress(ctx: CallContext) -> Result<JsObject> {
let dec = Decoder::new();
let decoder = Dec {
inner: dec,
data: data.into_ref()?,
data: data.into_value()?.to_vec(),
as_buffer,
};
ctx.env.spawn(decoder).map(|v| v.promise_object())
Expand Down

0 comments on commit ff72bdc

Please sign in to comment.