Search code examples
iosxcodeaddress-sanitizer

What exactly is the "memory history" command?


Summary: I got the message:

AddressSanitizer debugger support is active. Memory error breakpoint has been installed and you can now use it in the 'memory history' command.

What is the "memory history" command, and how exactly do I use it?

Details:

  • I had a bit of code that had a memory warning, which resulted to a crash.
  • I turned on the Address Sanitizer by going "Edit Scheme" > "Run" > "Enable Address Sanitizer".
  • Upon turning it on and running the same bit of code again, the crash didn't happen anymore, but the message appeared.

What does it mean, and what exactly is the memory history command? How do I use it? I have searched, but I haven't found anything that answers my question.


Solution

  • Running under Address Sanitizer allows you to see how were objects allocated. The memory history command needs a pointer/address and it will show how that object was allocated (a historical stacktrace of the allocation):

    (lldb) po self
    <MasterViewController: 0x61800000e080>
    
    (lldb) memory history 0x61800000e080
      thread ... name = 'Memory allocated at'
        frame #0: 0x00000001051bba97 libclang_rt.asan_iossim_dynamic.dylib`wrap_calloc + 199
        frame #1: 0x00000001064362fd libobjc.A.dylib`class_createInstance + 84
        frame #2: 0x0000000106440dc7 libobjc.A.dylib`_objc_rootAlloc + 41
        frame #3: 0x00000001072d6d25 UIKit`-[UIClassSwapper initWithCoder:] + 175
        frame #4: 0x00000001074c731b UIKit`UINibDecoderDecodeObjectForValue + 683
        ...
    

    It even works on already-deallocated objects, where it also shows the deallocation backtrace! This is extremely useful when you're accidentally accessing an already freed object.