I received an error
EXC_BAD_ACCESS code=2 at0xb0987654
I am wondering how to print out the value at 0xb0987654?
What does EXC_BAD_ACCESS mean? EXC_BAD_ACCESS is an exception raised as a result of accessing bad memory. We're constantly working with pointers to memory in Swift that link to a specific memory address. An application will crash whenever we try to access a pointer that is invalid or no longer exists.
EXC_BAD_ACCESS means that message was sent to a point in the memory where there's no instance of a class to execute it. Thus “bad access”. You will get EXC_BAD_ACCESS in 3 cases: An object is not initialized.
EXC_BAD_ACCESS KERN_INVALID_ADDRESS crash is not due to memory leak, but due to the attempt to access an deallocated object. Example: if you used __weak typeof(self) weakSelf = self; and object has been released before you accessing it inside block you'll got the crash.
Once an Objective-C or Swift object no longer has any strong references to it, the object is deallocated. Attempting to further send messages to the object as if it were still a valid object is a “use after free” issue, with the deallocated object still receiving messages called a zombie object.
To debug an EXC_BAD_ACCESS, you can generally find out the where the dangling pointer is by enabling zombie objects.
Xcode
Choose edit scheme, then Diagnostics tab in the Run section, then click the 'Zombie Objects' option.
AppCode
Choose edit target, and add the following environment variable:
NSZombieEnabled=YES
Another cause for EXC_BAD_ACCESS can be infinite recursion, which can be found by adding some logging.
Update for C++:
To debug dangling pointers in C++ with the Clang compiler try using Address Sanitizer (ASAN) from Google.
It looks like maybe you are trying to write onto a code page or something? EXC_BAD_ACCESS is described in /usr/include/mach/exception_types.h:
#define EXC_BAD_ACCESS 1 /* Could not access memory */ /* Code contains kern_return_t describing error. */ /* Subcode contains bad memory address. */
And from kern_return.h:
#define KERN_PROTECTION_FAILURE 2 /* Specified memory is valid, but does not permit the * required forms of access. */
You can see WHERE that address is in your binary by doing:
(lldb) image lookup -va 0xb0987654
But what you really need to figure out is who is trying to write there. If the problem is simple this might tell you what's wrong, but as Jasper suggests, this is probably some use-after-free or other such problem, and the bad actor is long gone by the time you crash. guardmalloc can also sometimes catch this sort of error (you can enable this in Xcode in the Run scheme.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With