Skip to content

Memory leak in napi::Error::from_reason #1903

@everplus

Description

@everplus

Test Case:

File: lib.rs

#![deny(clippy::all)]

#[macro_use]
extern crate napi_derive;

use napi::bindgen_prelude::*;

#[napi]
pub struct Test {}

#[napi]
impl Test {
  #[napi(constructor)]
  pub fn new() -> Result<Self> {
    Ok(Self {})
  }

  #[napi]
  pub async fn hello(&self) -> Result<()> {
    Err(napi::Error::from_reason("world"))
  }
}

impl Drop for Test {
  fn drop(&mut self) {
    println!("Test Dropped");
  }
}

File: test.js

const { Test } = require("./index.js");

const { setFlagsFromString } = require("v8");
const { runInNewContext } = require("vm");
let gc = global.gc;
if (!gc) {
  setFlagsFromString("--expose_gc");
  try {
    gc = runInNewContext("gc");
  } catch (_) {
    gc = () => {};
  }
}

async function test() {
  gc();
  let test = new Test();
  try {
    let result = await test.hello();
  } catch (err) {
    console.log(err.message);
  }
}

setInterval(test, 1000);

Result, Missing 'Test Dropped':

world
world
world
world
world
world

Chrome Inspector:

image

Is it possible that I'm not using it correctly?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions