Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

c++ - Node NAPI proper way to free up memory from dynamic library

I'm currently writing an module and a part of that loads a dynamic library and creates native workers to execute the functions of the dynamic library. The functions of the dynamic library are written in golang, they accept a char* and return a different char*. When i don't explicitly free the allocated memory in golang i get a memory leak. If i do, it crashes with exit codes like 3221225477 or 3221226356 after some time.

Therefore is my question how to properly free memory, allocated outside of napi from napi.

class DllFuncWorker: public Napi::AsyncWorker {
    public:
        DllFuncWorker(Napi::Function& callback, std::string config)
        : Napi::AsyncWorker(callback), config(config), response("")
        {
        }

        ~DllFuncWorker() {}
    
    protected:
    void Execute() override {
      if(hGetProcIDDLL == NULL){
        response = "{"Error":"dll was not loaded"}";
        return;
      }
      response = dllFunc(config.c_str());
      config.clear();
    }

    void OnError(const Napi::Error& e) override
    {
      Napi::Env env = Env();

      Callback().MakeCallback(
        Receiver().Value(),
        {
          e.Value(),
          env.Undefined()
        }
      );
    }

    void OnOK() override {
        Callback().Call({Env().Null(), Napi::String::New(Env(), response)});
    }

    private:
        char* response;
        std::string config;
};

Napi::Value queueDllFuncWorker(const Napi::CallbackInfo& info) {
  Napi::Env env = info.Env();
  if (info.Length() < 2) {
    Napi::TypeError::New(env, "Wrong number of arguments")
        .ThrowAsJavaScriptException();
    return env.Null();
  }

  if (!info[0].IsString()) {
    Napi::TypeError::New(env, "{"Error":"invalid config parameter"}").ThrowAsJavaScriptException();
    return env.Null();
  }

  if (!info[1].IsFunction()) {
    Napi::TypeError::New(env, "{"Error":"invalid callback function"}").ThrowAsJavaScriptException();
    return env.Null();
  }

  std::string str = info[0].ToString().Utf8Value();

  Napi::Function cb = info[1].As<Napi::Function>();

  DllFuncWorker* dllFuncWorker= new DllFuncWorker(cb, str);
  dllFuncWorker->Queue();
  return info.Env().Undefined();
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
  exports.Set(Napi::String::New(env, "dllFunc"), Napi::Function::New(env, queueDllFuncWorker));
  return exports;
}
question from:https://stackoverflow.com/questions/65939531/node-napi-proper-way-to-free-up-memory-from-dynamic-library

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...