EXC_BAD_ACCESS Error
EXC_BAD_ACCESS means the app tried to access invalid or deallocated memory.
It is one of the most common and serious crashes in iOS/macOS development.
Typical crash:
text id=”m7q2we”
Thread 1: EXC_BAD_ACCESS (code=1, address=0x…)
Common causes in iOS (Objective-C)
- Accessing deallocated object
objectivec id=”v4k8ra”
NSObject *obj = [[NSObject alloc] init];
[obj release];
[obj description];
The object was already freed from memory.
- Dangling pointer
objectivec id=”n3p1xd”
NSString *str;
{
NSString *temp = [[NSString alloc] initWithString:@”Hello”];
str = temp;
[temp release];
}
NSLog(@”%@”, str);
- Over-releasing object (Non-ARC)
objectivec id=”u8w5kc”
[obj release];
[obj release];
Double release can crash.
- Invalid array access
objectivec id=”g7q2mv”
NSArray *arr = @[@”A”];
NSLog(@”%@”, arr[5]);
Usually causes:
text id=”r4k9zn”
NSRangeException
but may also lead to memory issues.
- Accessing zombie object
Message sent to deallocated instance.
Example:
text id=”t2p5xe”
message sent to deallocated instance
- Unsafe C pointers
objectivec id=”q6m3yt”
char *ptr = NULL;
printf(“%s”, ptr);
How to debug EXC_BAD_ACCESS
Enable Zombies
In Xcode:
Steps
- Product
- Scheme
- Edit Scheme
- Diagnostics
- Enable:
text id=”w9k1vc”
Zombie Objects
This helps identify deallocated objects.
Common fixes
Use ARC
Enable:
text id=”p4q7ra”
Automatic Reference Counting
ARC prevents:
- over-release,
- memory leaks,
- dangling pointers. Use weak/strong properly Weak delegate
objectivec id=”j8m2kd”
@property (nonatomic, weak) id delegate;
Check object existence
objectivec id=”x5q7ne”
if(obj){
NSLog(@”%@”, obj);
}
Avoid manual memory management
If possible, avoid:
objectivec id=”f3k1wc”
retain
release
autorelease
under ARC projects.
Swift version
In Swift:
swift id=”b7p4xd”
unowned self
or unsafe pointers can also cause EXC_BAD_ACCESS.
Common crash messages
| Error | Meaning |
| — | – |
| message sent to deallocated instance | Object already freed |
| KERN_INVALID_ADDRESS | Invalid memory address |
| EXC_BAD_ACCESS code=1 | Bad memory access |
| SIGSEGV | Segmentation fault |
Useful tools
- Xcode official website
- Instruments documentation
- Firebase Crashlytics Best debugging workflow
- Enable Zombies
- Add Exception Breakpoint
- Check stack trace
- Inspect object lifecycle
- Verify ARC settings Difference between SIGABRT and EXC_BAD_ACCESS
| Crash | Meaning |
| – | – |
| SIGABRT | App aborted due to exception |
| EXC_BAD_ACCESS | Invalid memory access |