How to debug EXC_BAD_ACCESS in Objective C
Issue
In the Xcode IDE two environment variables are useful for getting further information on the memory allocation error EXC_BAD_ACCESS when using the simulator:
NSZombieEnabled=YES
MallocStackLoggingNoCompact=1
There variables should be unchecked when not debugging specific memory issues, as they will cause the memory shortages in the runtime environment, and should never be enabled in a shipping application. This is indicated by the following message:
"Data Formatters temporarily unavailable, will re-try after a 'continue'. (Not safe to call dlopen at this time.)"
Uncheck these environment variables to resolve the issue.
Resolution
To set these environment variables in the Xcode IDE.
In Xcode 3.2:
- In the left pane of Xcode expand
Executables
- Select the application executable
- Select
File |
Get Info
- Select the
Arguments segment control
- At the bottom of the
Variables to be set in the environment dialogue click
+
- Enter
NSZombieEnable for the name and
YES for the value
- Click
+ again
- Enter
MallocStackLoggingNoCompact for the name and
1 for the value
- Close the dialogue
In Xcode 4.0:
- Select
Product |
Edit Scheme from the menu
- Select the appropriate scheme (
Run/Test) in the left pane
- Select the
Arguments segment control
- At the bottom of the
Environment Variables dialogue click
+
- Enter
NSZombieEnable for the name and
YES for the value
- Click
+ again
- Enter
MallocStackLoggingNoCompact for the name and
1 for the value
- Close the dialogue
Recreate the fault. An error should appear in the debugger console (
Run |
Console).
*** -[__NSArrayI release]: message sent to deallocated instance 0x4d90af0
This address is then used to query the debugger with
info malloc-history:
(gdb) info malloc-history 0x4d90af0
That will produce the following output:
Alloc: Block address: 0x04d90af0 length: 32
Stack - pthread: 0xa0078540 number of frames: 37
0: 0x90e5a103 in malloc_zone_calloc
1: 0x5d862 in NSAllocateScannedUncollectable
2: 0xa659b in _NSInitializeCacheLine
3: 0xa6318 in _sortedObjectsUsingDescriptors
4: 0x127c89 in -[NSSet(NSKeyValueSorting) sortedArrayUsingDescriptors:]
5: 0x24b0d in -[Task getFullSession:intoArray:] at /Application/Task.m:177
6: 0x604d in -[AViewController viewDidLoad] at /Application/Classes/AViewController.m:50
7: 0x3a965e in -[UIViewController view]
8: 0x3a7a57 in -[UIViewController contentScrollView]
9: 0x3b8201 in -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:]
10: 0x3b6831 in -[UINavigationController _layoutViewController:]
11: 0x3b7b4c in -[UINavigationController _startTransition:fromViewController:toViewController:]
12: 0x3b2606 in -[UINavigationController _startDeferredTransitionIfNeeded]
13: 0x4cae01 in -[UILayoutContainerView layoutSubviews]
14: 0x1e87451 in -[CALayer layoutSublayers]
15: 0x1e8717c in CALayerLayoutIfNeeded
16: 0x1e87088 in -[CALayer layoutIfNeeded]
17: 0x3aab5f in -[UIViewController window:willAnimateRotationToInterfaceOrientation:duration:]
18: 0x3238e9 in -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:]
19: 0x5a2948 in -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:]
20: 0x3ad982 in -[UIViewController presentModalViewController:withTransition:]
21: 0x3a6269 in -[UIViewController _tryRecursivelyPresentModalViewController:withTransition:]
22: 0x3ad085 in -[UIViewController presentModalViewController:withTransition:]
23: 0x4bc0 in -[RootViewController tableView:didSelectRowAtIndexPath:] at /Application/Classes/RootViewController.m:98
24: 0x371794 in -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:]
25: 0x367d50 in -[UITableView _userSelectRowAtPendingSelectionIndexPath:]
26: 0x7a7f6 in __NSFireDelayedPerform
27: 0x128afe3 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__
28: 0x128c594 in __CFRunLoopDoTimer
29: 0x11e8cc9 in __CFRunLoopRun
30: 0x11e8240 in CFRunLoopRunSpecific
31: 0x11e8161 in CFRunLoopRunInMode
32: 0x1ae9268 in GSEventRunModal
33: 0x1ae932d in GSEventRun
34: 0x30a42e in UIApplicationMain
35: 0x2914 in main at /Application/main.m:14
36: 0x28a5 in start
Dealloc: Block address: 0x04d90af0
Stack - pthread: 0xa0078540 number of frames: 35
0: 0x90e5e1ad in malloc_zone_free
1: 0xa64bb in _sortedObjectsUsingDescriptors
2: 0x127c89 in -[NSSet(NSKeyValueSorting) sortedArrayUsingDescriptors:]
3: 0x24b0d in -[Task getFullSession:intoArray:] at /Application/Task.m:177
4: 0x604d in -[AViewController viewDidLoad] at /Application/Classes/AViewController.m:50
5: 0x3a965e in -[UIViewController view]
6: 0x3a7a57 in -[UIViewController contentScrollView]
7: 0x3b8201 in -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:]
8: 0x3b6831 in -[UINavigationController _layoutViewController:]
9: 0x3b7b4c in -[UINavigationController _startTransition:fromViewController:toViewController:]
10: 0x3b2606 in -[UINavigationController _startDeferredTransitionIfNeeded]
11: 0x4cae01 in -[UILayoutContainerView layoutSubviews]
12: 0x1e87451 in -[CALayer layoutSublayers]
13: 0x1e8717c in CALayerLayoutIfNeeded
14: 0x1e87088 in -[CALayer layoutIfNeeded]
15: 0x3aab5f in -[UIViewController window:willAnimateRotationToInterfaceOrientation:duration:]
16: 0x3238e9 in -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:]
17: 0x5a2948 in -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:]
18: 0x3ad982 in -[UIViewController presentModalViewController:withTransition:]
19: 0x3a6269 in -[UIViewController _tryRecursivelyPresentModalViewController:withTransition:]
20: 0x3ad085 in -[UIViewController presentModalViewController:withTransition:]
21: 0x4bc0 in -[RootViewController tableView:didSelectRowAtIndexPath:] at /Application/Classes/RootViewController.m:98
22: 0x371794 in -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:]
23: 0x367d50 in -[UITableView _userSelectRowAtPendingSelectionIndexPath:]
24: 0x7a7f6 in __NSFireDelayedPerform
25: 0x128afe3 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__
26: 0x128c594 in __CFRunLoopDoTimer
27: 0x11e8cc9 in __CFRunLoopRun
28: 0x11e8240 in CFRunLoopRunSpecific
29: 0x11e8161 in CFRunLoopRunInMode
30: 0x1ae9268 in GSEventRunModal
31: 0x1ae932d in GSEventRun
32: 0x30a42e in UIApplicationMain
33: 0x2914 in main at /Application/main.m:14
34: 0x28a5 in start
Alloc: Block address: 0x04d90af0 length: 24
Stack - pthread: 0xa0078540 number of frames: 40
0: 0x90e5a103 in malloc_zone_calloc
1: 0x90e5a05a in calloc
2: 0x13fdd0f in _internal_class_createInstanceFromZone
3: 0x140087d in class_createInstance
4: 0x123e687 in __CFAllocateObject2
5: 0x12a2d64 in +[__NSArrayI __new::]
6: 0x121faba in +[NSArray arrayWithObjects:count:]
7: 0x127ca9 in -[NSSet(NSKeyValueSorting) sortedArrayUsingDescriptors:]
8: 0x24b0d in -[Task getFullSession:intoArray:] at /Application/Task.m:177
9: 0x604d in -[AViewController viewDidLoad] at /Application/Classes/AViewController.m:50
10: 0x3a965e in -[UIViewController view]
11: 0x3a7a57 in -[UIViewController contentScrollView]
12: 0x3b8201 in -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:]
13: 0x3b6831 in -[UINavigationController _layoutViewController:]
14: 0x3b7b4c in -[UINavigationController _startTransition:fromViewController:toViewController:]
15: 0x3b2606 in -[UINavigationController _startDeferredTransitionIfNeeded]
16: 0x4cae01 in -[UILayoutContainerView layoutSubviews]
17: 0x1e87451 in -[CALayer layoutSublayers]
18: 0x1e8717c in CALayerLayoutIfNeeded
19: 0x1e87088 in -[CALayer layoutIfNeeded]
20: 0x3aab5f in -[UIViewController window:willAnimateRotationToInterfaceOrientation:duration:]
21: 0x3238e9 in -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:]
22: 0x5a2948 in -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:]
23: 0x3ad982 in -[UIViewController presentModalViewController:withTransition:]
24: 0x3a6269 in -[UIViewController _tryRecursivelyPresentModalViewController:withTransition:]
25: 0x3ad085 in -[UIViewController presentModalViewController:withTransition:]
26: 0x4bc0 in -[RootViewController tableView:didSelectRowAtIndexPath:] at /Application/Classes/RootViewController.m:98
27: 0x371794 in -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:]
28: 0x367d50 in -[UITableView _userSelectRowAtPendingSelectionIndexPath:]
29: 0x7a7f6 in __NSFireDelayedPerform
30: 0x128afe3 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__
31: 0x128c594 in __CFRunLoopDoTimer
32: 0x11e8cc9 in __CFRunLoopRun
33: 0x11e8240 in CFRunLoopRunSpecific
34: 0x11e8161 in CFRunLoopRunInMode
35: 0x1ae9268 in GSEventRunModal
36: 0x1ae932d in GSEventRun
37: 0x30a42e in UIApplicationMain
38: 0x2914 in main at /Application/main.m:14
39: 0x28a5 in start
The cause of this error is visible near the top of the block:
...
4: 0x127c89 in -[NSSet(NSKeyValueSorting) sortedArrayUsingDescriptors:]
5: 0x24b0d in -[Task getFullSession:intoArray:] at /Application/Task.m:177
...
2: 0x127c89 in -[NSSet(NSKeyValueSorting) sortedArrayUsingDescriptors:]
3: 0x24b0d in -[Task getFullSession:intoArray:] at /Application/Task.m:177
...
7: 0x127ca9 in -[NSSet(NSKeyValueSorting) sortedArrayUsingDescriptors:]
8: 0x24b0d in -[Task getFullSession:intoArray:] at /Application/Task.m:177
...
References
Products
Apple Xcode 4.0 (iOS SDK 4.3)
Apple Xcode 3.2 (iOS SDK 4.2)
Created: 18th February 2011
Updated: 25th June 2011
© 2005-2024 Jamie Morrison