nodejs / nodejs/node-addon-api
Supporting circular references that can be garbage collected.
Personne n'a encore pris cette issue.
- Langage dominant
- C++
- Étoiles
- 2.4k
- Forks
- 499
- Merge moyen
- 2 j 11 h
- PR mergées (30 j)
- 2
Description
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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par l’utilisation de Napi::ObjectWrap et Napi::Reference présentée dans l’issue, puis étudiez la manière dont Node-API gère les références natives et le garbage collection pour des objets se référençant mutuellement. Le travail est terminé lorsqu’il a été établi si les références circulaires pouvant être récupérées sont prises en charge, et que l’approche nécessaire au niveau de l’API a été documentée ou proposée ; l’issue ne nomme aucun fichier source ni aucun test.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- cpp, nodejs
- Domaine
- backend
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- À clarifier
- Accessibilité débutants
- 25/100