nodejs / nodejs/node-addon-api
Supporting circular references that can be garbage collected.
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- C++
- Star
- 2.4k
- Fork
- 499
- Merge trung bình
- 2 ngày 11 giờ
- Pull request đã merge (30 ngày)
- 2
Mô tả
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.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với cách sử dụng Napi::ObjectWrap và Napi::Reference được nêu trong issue, sau đó tìm hiểu cách Node-API xử lý các tham chiếu native và việc thu gom rác cho các đối tượng tham chiếu lẫn nhau. Được xem là hoàn tất khi xác định được liệu các tham chiếu vòng có thể được thu gom có được hỗ trợ hay không, đồng thời tài liệu hóa hoặc đề xuất cách tiếp cận cần thiết ở cấp API; issue không nêu tên tệp nguồn hay bài kiểm thử nào.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- cpp, nodejs
- Lĩnh vực
- backend
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100