nodejs / nodejs/node-addon-api
Supporting circular references that can be garbage collected.
还没有人认领这个 Issue。
- 主要语言
- C++
- 星标
- 2.4k
- 派生
- 499
- 平均合并
- 2 天 11 小时
- 30 天内合并 PR
- 2
描述
In JavaScript I can make two objects that reference each other and they'll still be GCed.
function makeObjectsThatReferenceEachOtherButLeakNoReferences() {
const a = new Uint8Array(1024);
const b = new Uint8Array(1024);
a.other = b; // make them reference each other
b.other = a;
}
makeObjectsThatReferenceEachOtherButLeakNoReferences();
In the code above, even though a circular reference was created, JavaScript will see there is no path from root and garbage collect the objects.
Is it possible to do the same in C++ Napi. If I make a class
class MyClass : public Napi::ObjectWrap<MyClass> {
...
Napi::Reference<Napi::Object> storedObjectRef_;
};
And I manage to make 2 instanced of MyClass and set storedObjectRef_ so they point to each other, AFAICT these objects will never be garbage collected.
Is there a solution?
Note: I know I could add some function close or whatever to null out storedObjectRef_ but that's not really the question I'm asking. I'm trying to reproduce JS garbage collecting circular references.
One idea I guess, which appears to work, is I could add a JS property to MyClass. So instead of Napi::Reference<Napi::Object> storageObjectRef_ I'd use Get, Set as in
this->Value().Set("storageObjectRef", otherObject);
but unfortunately that's visible externally which I don't want. I could use a symbol but those are inspectable too. Though it might be better than nothing if there is no other solutions.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 issue 中所示的 Napi::ObjectWrap 和 Napi::Reference 用法开始,然后调查 Node-API 如何处理相互引用对象的原生引用和垃圾回收。完成的标准是确定是否支持可回收的循环引用,并记录或提出所需的 API 级方案;该 issue 未指定源文件或测试。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- cpp, nodejs
- 领域
- backend
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100