1
votes

When i run my program, I have this error :

a.out(56815,0x10bb2c5c0) malloc: *** error for object 0x7fe5ea402a90: pointer being freed was not allocated a.out(56815,0x10bb2c5c0) malloc: *** set a breakpoint in malloc_error_break to debug

how i can set a breakpoint to malloc_error_break with lldb ?

thanks for the help

1
It's probably not very meaningful to debug malloc... the root of this bug is somewhere in your program. My guts tell me it will be found at that one place where you do strange and/or needlessly complicated things with pointers. - Lundin

1 Answers

1
votes
(lldb) break set -n malloc_error_break

is the lldb command for setting a breakpoint by symbol name.

That breakpoint will stop you at the point where the errant free occurred, which is some information. It may be for instance that you have a code path where you don't initialize something that you later free. The backtrace when you hit malloc_error_break will show you what you are trying to free, so you can trace back why it didn't get initialized.

If the problem ends up being more complicated (a double-free for instance), that's going to be a little harder to track down from the stop point, since you can't tell from there where the first free was. In that case, it's definitely worthwhile to rebuild your program with ASAN enabled and run it again in the debugger. In the case of double free's and such-like since ASAN records the whole malloc history of the program, it can tell you every time the errant pointer was handled by the malloc system, making the error easier to spot. It also does a bunch of pre-flight checks and can often catch memory errors early on. And at a surprisingly small performance cost as well...

There's more on ASAN here:

https://clang.llvm.org/docs/AddressSanitizer.html

If it's too difficult to rebuild with ASAN you can use the MallocStackLoggingNoCompact environment variable and then use the malloc_history program to print the errors. There's more info about that in the "malloc" manpage, e.g.:

https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/malloc.3.html

But if you are planning to do more than a little development, it's a good idea to get familiar with ASAN. It makes tracking down allocation & deallocation errors much easier.