Skip to content
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

Fix a couple of cpu profile memory leaks #218

Merged
merged 1 commit into from
Oct 28, 2021

Conversation

dylanahsmith
Copy link
Collaborator

Problem

I tried using valgrind to check for leaks in v8go and found some in the cpu profiling code that can be reproduced using go test -c && valgrind --leak-check=full --show-leak-kinds=definite --track-origins=yes ./v8go.test -test.run=TestCPUProfile (run on linux with valgrind already installed)

Here are the unique leaks that valgrind detected

==321754== 8 bytes in 1 blocks are definitely lost in loss record 569 of 894
==321754==    at 0x483C583: operator new[](unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==321754==    by 0xA8169D: NewCPUProfileNode (v8go.cc:239)
==321754==    by 0xA89EC4: CPUProfilerStopProfiling (v8go.cc:275)
==321754==    by 0xA7F55E: _cgo_eb9c209376d5_Cfunc_CPUProfilerStopProfiling (cgo-gcc-prolog:78)
==321754==    by 0x8705AF: runtime.asmcgocall.abi0 (asm_amd64.s:765)
==321754==    by 0xC000027FFF: ???
==321754==    by 0x2DFECDD7: ???
==321754==    by 0x849117: runtime.exitsyscallfast.func1 (proc.go:4017)
==321754==    by 0x86E708: runtime.systemstack.abi0 (asm_amd64.s:383)
==321754==
==321754== 8 bytes in 2 blocks are definitely lost in loss record 570 of 894
==321754==    at 0x483C583: operator new[](unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==321754==    by 0xA8169D: NewCPUProfileNode (v8go.cc:239)
==321754==    by 0xA816BB: NewCPUProfileNode (v8go.cc:241)
==321754==    by 0xA89EC4: CPUProfilerStopProfiling (v8go.cc:275)
==321754==    by 0xA7F55E: _cgo_eb9c209376d5_Cfunc_CPUProfilerStopProfiling (cgo-gcc-prolog:78)
==321754==    by 0x8705AF: runtime.asmcgocall.abi0 (asm_amd64.s:765)
==321754==    by 0x1A0C7BF: ???
==321754==    by 0x1: ???
==321754==    by 0xC000044DCF: ???

Solution

I added the missing delete for the C++ allocated memory and free for the malloc allocated memory.

@codecov
Copy link

codecov bot commented Oct 28, 2021

Codecov Report

Merging #218 (be55804) into master (cc3b2e8) will not change coverage.
The diff coverage is n/a.

❗ Current head be55804 differs from pull request most recent head 9af31fd. Consider uploading reports for the commit 9af31fd to get more accurate results
Impacted file tree graph

@@           Coverage Diff           @@
##           master     #218   +/-   ##
=======================================
  Coverage   95.59%   95.59%           
=======================================
  Files          16       16           
  Lines         545      545           
=======================================
  Hits          521      521           
  Misses         15       15           
  Partials        9        9           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cc3b2e8...9af31fd. Read the comment docs.

v8go.cc Outdated
@@ -286,6 +286,7 @@ void CPUProfileNodeDelete(CPUProfileNode* node) {
CPUProfileNodeDelete(node->children[i]);
}

delete node->children;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing to

Suggested change
delete node->children;
delete[] node->children;

to avoid a Mismatched free() / delete / delete [] error

@dylanahsmith dylanahsmith force-pushed the fix-cpu-profile-mem-leaks branch from be55804 to 9af31fd Compare October 28, 2021 18:44
@dylanahsmith
Copy link
Collaborator Author

I reran valgrind with go test -c && valgrind --leak-check=full --show-leak-kinds=definite --track-origins=yes ./v8go.test -test.run=TestCPUProfile and it didn't find any leaks.

@dylanahsmith dylanahsmith merged commit 3e22137 into master Oct 28, 2021
@dylanahsmith dylanahsmith deleted the fix-cpu-profile-mem-leaks branch October 28, 2021 18:56
@genevieve
Copy link
Collaborator

genevieve commented Nov 4, 2021

I wonder if it would make sense to document this in a DEVELOPMENT type doc and/or run valgrind in our ci flow against the whole test suite. Not everyone might have it installed on their machines and it would be a helpful ci check on PRs

@genevieve genevieve mentioned this pull request Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants