Debug memory leaks in cgo calls

1,211 views
Skip to first unread message

Pablo Rozas Larraondo

unread,
Dec 14, 2016, 7:58:57 PM12/14/16
to golang-nuts
I've seen this question asked before in the mail list but with no clear answers. I'm just asking it again in case someone has come up with a new way of doing this:

Does anyone know about a good tool or method to detect memory leaks happening on C code being called by cgo? 

Cheers,
Pablo

Ian Lance Taylor

unread,
Dec 14, 2016, 8:36:31 PM12/14/16
to Pablo Rozas Larraondo, golang-nuts
clang's address sanitizer can do memory leak detection. I have not
tried that with Go myself, but I suggest starting with that to see if
you can make it work.

Ian

Pablo Rozas Larraondo

unread,
Dec 15, 2016, 2:15:23 PM12/15/16
to Ian Lance Taylor, golang-nuts
Thanks Ian,

So if I understand it well, this involves using clang compiler instead of the default gcc when building Go code that uses cgo. I'll do some research on how to do this.

Cheers,
Pablo

Marko Kevac

unread,
Dec 16, 2016, 5:34:28 AM12/16/16
to golang-nuts, ia...@golang.org
There is an address sanitizer in gcc too.

Will Newton

unread,
Dec 16, 2016, 5:51:27 AM12/16/16
to Pablo Rozas Larraondo, golang-nuts
I'm not aware of any really nice tool to do this. clang's memory
sanitizer may work (I think last time I looked the gcc equivalent was
not capable of doing leak detection). You can override the system
malloc with e.g. jemalloc which will give you more debug information
than the system malloc. Unfortunately most allocators leak detection
is done using an atexit handler and I didn't find a nice way to get Go
to run these handlers in my application. It is possible to run
jemalloc in a mode that dumps periodic heap traces to disk which you
can then diff to figure out which parts of the heap are growing.
Reply all
Reply to author
Forward
0 new messages