ios crash EXC_BAD_ACCESS KERN_INVALID_ADDRESS ios crash EXC_BAD_ACCESS KERN_INVALID_ADDRESS ios ios

ios crash EXC_BAD_ACCESS KERN_INVALID_ADDRESS


This crash occurs due to a dangling pointer. When any variable or object is trying to access an object that's already been deallocated, this crash occurs.


This is an old question but the EXC_BAD_ACCESS KERN_INVALID_ADDRESS crash is not due to memory leak, but due to the attempt to access an deallocated object. Because the memory of the deallocated object is possibly now occupied by another object of different type, the last line in the crash stack might be mis-leading because it's not really part of your programmed execution path, so usually we need to look up the trace a little bit. However the stack trace posted by @Sj consists basically only of system calls so it's really hard. If this is generated in a debug session, adding an "All Exceptions" breakpoint might help generate a more informative stack trace.


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. The reason — access to wrong memory address because object was deallocated.

To prevent this use __strong typeof(self) strongSelf = self; inside the block. Nil value will be properly handled without crash


Note: use this code sample for fast work.

#define weakify(var) __weak typeof(var) AHKWeak_##var = var;#define strongify(var) \_Pragma("clang diagnostic push") \_Pragma("clang diagnostic ignored \"-Wshadow\"") \__strong typeof(var) var = AHKWeak_##var; \_Pragma("clang diagnostic pop")

Usage example:

weakify(self); // Remove retain cycle[self someFunctionWithBlock:^{    strongify(self); // Make reference to address valid}];