4 ms·
Use address sanitizer and leak sanitizer (-fsanitize=address). Compile with options that forbid omission of frame pointers (-fno-omit-frame-pointer) to get a ni
by kccqzy 10y ago
Use address sanitizer and leak sanitizer (-fsanitize=address). Compile with options that forbid omission of frame pointers (-fno-omit-frame-pointer) to get a nice backtrace of where the allocation occurred. Also compile with full debugging info (-g) so that leak errors contain file and line numbers. Problem solved.
EDIT: this apparently only works on x86-64 Linux. If you are targeting something else, it might be slightly harder.
- derefr 10y agoUnless you're working in the embedded space, there's no reason to not write your C project to target whatever arch is the most debug-friendly first, and then just port it to the system it's "for" at the end.
- jseaidou 10y agoUnless the issue ends up being arch specific
- 2trill2spill 10y ago> EDIT: this apparently only works on x86-64 Linux. If you are targeting something else, it might be slightly harder. What? I do the same thing on both FreeBSD and macOS.
- slavik81 10y agoI think he got that from the Clang documentation: https://clang.llvm.org/docs/LeakSanitizer.html https://clang.llvm.org/docs/LeakSanitizer.html
- problems 10y agoHow does asan compare with Valgrind? Valgrind is a serious savior for me sometimes, it's able to catch leaks, access violations, use of uninitialized memory, use-after-frees (and it can tell you where it was free'd), data races, etc. Microsoft's heap debugging stuff really doesn't measure up unfortunately, on Windows I'm frequently left grinding my face into the ground when I hit such issues. Fortunately they're fairly rare as I generally stick to C++ with more modern techniques which avoid most of the pitfalls - but when they do happen it can get nasty without a tool like Valgrind. Helgrind is quite good for threading issues too.
- slavik81 10y agoValgrind misses certain problems with stack memory and runs your program with a 10x slowdown. AddressSanitizer and friends use a lot of extra virtual address space, but run with about a 2x slowdown. If you're interested in C++ tooling, maybe watch through The Care and Feeding of C++'s Dragons[1] from GoingNative 2013. They start to talk about AddressSanitizer at 55m40s, but the entire thing is really good. The tools they talk about have just gotten even better and more widely available since then. [1]: https://channel9.msdn.com/Events/GoingNative/2013/The-Care-and-Feeding-of-C-s-Dragons https://channel9.msdn.com/Events/GoingNative/2013/The-Care-a...
- problems 10y agoInteresting, what I'd heard from a colleague is that asan wasn't worth using since it couldn't catch uninitialized memory and stuff. Thanks, I'll have to look into it more.
- slavik81 10y agoThere are different sanitizers for catching different sorts of bugs. MemorySanitizer is the one that catches use of uninitialized memory.
- MaulingMonkey 10y agoAddressSanitizer is part of a suite of tools - MemorySanitizer will catch uninitialized memory, and there is UndefinedBehaviorSanitizer and ThreadSanitizer as well. Your colleague may be complaining that a wrench is not a screwdriver. Address sanitizer has caught something on every codebase I've enabled it on. YMMV, but I encourage you to at least try it out and draw your own conclusions.
- MaulingMonkey 10y ago> Problem solved. You forgot "bypass or instrument our custom allocators", "determine repro steps in the first place", "filter out false positives", "bypass or instrument your refcounting - because failing to pair a free() with your malloc() isn't the problem, nor even failing to pair decRef()s with addRef()s - breaking up cycles is", etc. Sometimes the answer is "force your scripting language to garbage collect again". Sometimes the answer is "track down an incorrect cast in code that ultimately causes a single byte corruption in your refcount". Sometimes the answer is far more horrifying.