Conversation
The serialize() call returns a heap-allocated slice that was never freed after being copied into the NAPI ArrayBuffer by numberSliceToNapiValue. Added defer allocator.free(result) to prevent the leak.
Summary of ChangesHello @GrapeBaBa, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical memory leak occurring in the NAPI binding for Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly fixes a memory leak in BeaconStateView_serialize by adding a defer statement to free the heap-allocated slice. The other changes are minor formatting adjustments. I have added one comment regarding adherence to the repository's style guide.
| pub fn BeaconStateView_serialize(env: napi.Env, cb: napi.CallbackInfo(0)) !napi.Value { | ||
| const cached_state = try env.unwrap(CachedBeaconState, cb.this()); | ||
| const result = try cached_state.state.serialize(allocator); | ||
| defer allocator.free(result); |
There was a problem hiding this comment.
The repository style guide recommends grouping resource allocation and deallocation with newlines, specifically mentioning a newline before the allocation and after the defer statement. While a newline is correctly added after the defer, a newline is missing before the allocation on line 874.
References
- Use newlines to group resource allocation and deallocation, i.e. before the resource allocation and after the corresponding defer statement, to make leaks easier to spot. (link)
There was a problem hiding this comment.
Pull request overview
This PR fixes a memory leak in the NAPI binding for BeaconStateView_serialize by adding defer allocator.free(result) to properly deallocate the heap-allocated slice returned by serialize() after it has been copied into a NAPI ArrayBuffer. The PR also includes minor whitespace formatting fixes in test files.
Changes:
- Fixed memory leak in
BeaconStateView_serializeby freeing the allocated serialization buffer - Removed trailing whitespace in test case definitions
Reviewed changes
Copilot reviewed 1 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| bindings/napi/BeaconStateView.zig | Added defer allocator.free(result) to prevent memory leak when serializing beacon state |
| src/ssz/type/list.zig | Removed trailing whitespace before line continuation in test case |
| src/ssz/type/byte_list.zig | Removed trailing whitespace before line continuation in test cases |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: Chen Kai <[email protected]>
Motivation
The
serialize()call returns a heap-allocated slice that was never freed after being copied into the NAPI ArrayBuffer bynumberSliceToNapiValue. Addeddefer allocator.free(result)to prevent the leak.