diff --git a/README.md b/README.md index fde5204..f49dd1a 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ A module written in Rust and N-API provides interface (FFI) features for Node.js This module aims to provide similar functionality to the node-ffi module but with a completely rewritten underlying codebase. The node-ffi module has been unmaintained for several years and is no longer usable, so ffi-rs was developed to fill that void. -## features +## Features - High performance ✨ - Better type hints 🧐 @@ -47,7 +47,7 @@ Finished 2 cases! ``` -## changelog +## Changelog See [CHANGELOG.md](./CHANGELOG.md) @@ -710,6 +710,29 @@ load({ // { errnoCode: 22, errnoMessage: 'Invalid argument (os error 22)', value: -1 } ``` +## Memory Management + +It's important to free the memory allocations during a single ffi call prevent memory leak. + +What kinds of data memory are allocated in this? + +- call parameters in Rust environment which are allocated in the heap like `String` +- return value which in C environment which are allocated in the heap like `char*` + +At default, `ffi-rs` will free all of memory both call parameters and return value(except functionConstructor). + +In some cases, the called c function also free memory has been allocated after ffi-call will cause repeated release error. + +For avoid this error, there are two ways to prevent `ffi-rs` free memory automatically + +- set `freeResultMemory: false` when call `load` method + +If you set freeResultMemory to false, `ffi-rs` will not release the return result memory which was allocated in c environment + +- Use `DataType.External` as paramsType or retType + +If developers use `DataType.External` as paramsType or retType, please use `freePointer` to release the memory of pointer when this memory is no longer in use. ref [test.ts](./test.ts#170) + ## runInNewThread `ffi-rs` support run ffi task in a new thread without blocking the main thread which is useful for cpu intensive task. diff --git a/package.json b/package.json index ed05e04..f33d620 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "test": "esno ./test.ts", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add . && git commit -m \"docs: update changelog.md\" && git push origin master", "pub": "npm version patch && git push origin master --tags && npm run changelog", - "pub:alpha": "npm version prerelease --preid=alpha && git push origin master --tags && npm run changelog" + "pub:alpha": "npm version prerelease --preid=alpha && git push origin master --tags" }, "optionalDependencies": { "@yuuang/ffi-rs-darwin-arm64": "1.0.55",