nodejs / nodejs/node-addon-api

Supporting circular references that can be garbage collected.

オープン
#1,665 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

never-stale
主要言語
C++
スター
2.4k
フォーク
499
平均マージ
2日 11時間
マージ済み PR(30日)
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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Issue に示されている Napi::ObjectWrap と Napi::Reference の使用方法から始め、相互に参照するオブジェクトについて、Node-API がネイティブ参照とガベージコレクションをどのように処理するかを調査します。回収可能な循環参照がサポートされているかを明らかにし、必要な API レベルのアプローチを文書化または提案できれば完了です。この issue ではソースファイルもテストも指定されていません。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
cpp, nodejs
領域
backend
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。