-
Notifications
You must be signed in to change notification settings - Fork 698
Fix memory leak on context shutdown #1856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ianhattendorf
merged 5 commits into
nodegit:master
from
AlexaXs:fix/memory-leak-when-closing-context
Oct 27, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
797d074
Free memory when closing context
AlexaXs 4780b6e
Revert "Check if module is compiled under context aware node version"
AlexaXs 46f57c9
prepare code to add test
AlexaXs e3c8dde
test checking that objects are being tracked
AlexaXs dda1e2b
changes reviewed
AlexaXs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #ifndef TRACKERWRAP_H | ||
| #define TRACKERWRAP_H | ||
|
|
||
| #include <nan.h> | ||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| namespace nodegit { | ||
| // Base class used to track wrapped objects, so that we can | ||
| // free the objects that were not freed at the time of context | ||
| // closing (because their WeakCallback didn't trigger. See | ||
| // https://github.com/nodejs/help/issues/3297). | ||
| // Implementation based on node.js's class RefTracker (napi). | ||
| class TrackerWrap : public Nan::ObjectWrap { | ||
| public: | ||
| TrackerWrap() = default; | ||
| virtual ~TrackerWrap() = default; | ||
| TrackerWrap(const TrackerWrap &other) = delete; | ||
| TrackerWrap(TrackerWrap &&other) = delete; | ||
| TrackerWrap& operator=(const TrackerWrap &other) = delete; | ||
| TrackerWrap& operator=(TrackerWrap &&other) = delete; | ||
|
|
||
| // aliases: | ||
| // 'TrackerList': used in functionality related to a list. | ||
| // 'TrackerWrap' used in functionality not related to a list. | ||
| using TrackerList = TrackerWrap; | ||
|
|
||
| // Links 'this' right after 'listStart' | ||
| inline void Link(TrackerList* listStart) { | ||
| m_prev = listStart; | ||
| m_next = listStart->m_next; | ||
| if (m_next != nullptr) { | ||
| m_next->m_prev = this; | ||
| } | ||
| listStart->m_next = this; | ||
| } | ||
|
|
||
| // Unlinks itself from the list it's linked to | ||
| inline TrackerWrap* Unlink() { | ||
| if (m_prev != nullptr) { | ||
| m_prev->m_next = m_next; | ||
| } | ||
| if (m_next != nullptr) { | ||
| m_next->m_prev = m_prev; | ||
| } | ||
| m_prev = nullptr; | ||
| m_next = nullptr; | ||
| return this; | ||
| } | ||
|
|
||
| inline void SetTrackerWrapOwners(std::unique_ptr< std::vector<TrackerWrap*> > &&owners) { | ||
| m_owners = std::move(owners); | ||
| } | ||
|
|
||
| inline const std::vector<TrackerWrap*>* GetTrackerWrapOwners() const { | ||
| return m_owners.get(); | ||
| } | ||
|
|
||
| // Unlinks and returns the first item of 'listStart' | ||
| static TrackerWrap* UnlinkFirst(TrackerList *listStart); | ||
|
|
||
| // Returns number of items following 'listStart' | ||
| static int SizeFromList(TrackerList *listStart); | ||
|
|
||
| // Deletes items following 'listStart', but not 'listStart' itself | ||
| static void DeleteFromList(TrackerList *listStart); | ||
|
|
||
| private: | ||
| TrackerList* m_next {}; | ||
| TrackerList* m_prev {}; | ||
| // m_owners will store pointers to native objects | ||
| std::unique_ptr< std::vector<TrackerWrap*> > m_owners {}; | ||
| }; | ||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.