Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OS: Leaks Sanitizer

Mac OS X Sierra 10.13

I do as wrote here https://clang.llvm.org/docs/LeakSanitizer.html

I.e. created the small application with memory leak

#include <stdlib.h>
void *p;
int main() {
  p = malloc(7);
  p = 0; // The memory is leaked here.
  return 0;
}

Then build it and run to test how the memory leak is detected:

admins-Mac:test2 admin$ clang -fsanitize=address -g mleak.c ; ASAN_OPTIONS=detect_leaks=1 ./a.out
==556==AddressSanitizer: detect_leaks is not supported on this platform.
Abort trap: 6
admins-Mac:test2 admin$ 

How I can detect the leak? I need to use it for my large application.

like image 594
ZedZip Avatar asked Nov 24 '18 08:11

ZedZip


People also ask

What is leak sanitizer?

LeakSanitizer is a run-time memory leak detector. It can be combined with AddressSanitizer to get both memory error and leak detection, or used in a stand-alone mode. LSan adds almost no performance overhead until the very end of the process, at which point there is an extra leak detection phase.

How do I turn off AddressSanitizer?

AddressSanitizer can optionally detect stack use after return problems. This is available by default, or explicitly ( -fsanitize-address-use-after-return=runtime ). To disable this check at runtime, set the environment variable ASAN_OPTIONS=detect_stack_use_after_return=0 .

What is AddressSanitizer error?

Introduction. AddressSanitizer is a fast memory error detector. It consists of a compiler instrumentation module and a run-time library. The tool can detect the following types of bugs: Out-of-bounds accesses to heap, stack and globals.

What does AddressSanitizer do?

AddressSanitizer (or ASan) is an open source programming tool that detects memory corruption bugs such as buffer overflows or accesses to a dangling pointer (use-after-free). AddressSanitizer is based on compiler instrumentation and directly mapped shadow memory.


1 Answers

Note that you can also use the Leaks Instrument which ships with Xcode to find leaks in your code without having to install anything extra. It's not very well advertised, but it's a very useful tool. From the "Product" menu, choose "Profile". This may rebuild your application, and it will then launch the Instruments.app. You'll be presented with a sheet of different profiling instruments you can use like this:

The profile chooser for Instruments.app

Once you choose it and press the "record" button, it will run your app and display a track showing you any leaks, like this:

The main window of the Leaks instrument in Instruments.app

A green check means no leaks in the app at that time. A red "x" means a new leak since the last check. A gray "-" means there are leaks, but no new ones since the last check. The default is to check every 10 seconds.

At the bottom is a list of the current leaks. If you click on one, you'll see a stack trace on the right showing which function allocated the leaked memory.

This is a very powerful tool that has almost no documentation! I'm not sure why Apple keeps it so hidden.

like image 50
user1118321 Avatar answered Oct 27 '22 20:10

user1118321