xcode 从 NSString 调试 EXC_BAD_ACCESS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2360273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Debugging EXC_BAD_ACCESS from a NSString
提问by Thomas
I had a EXC_BAD_ACCESS message in my console. I read about the environment variables NSZombieEnabled and MallocStackLoggingNoCompact on this site. I created my environment variables: NSZombieEnabled = YESand MallocStackLoggingNoCompact = 1. In the console, I saw
我的控制台中有一条 EXC_BAD_ACCESS 消息。我在此站点上阅读了有关环境变量 NSZombieEnabled 和 MallocStackLoggingNoCompact 的信息。我创建了我的环境变量:NSZombieEnabled = YES和MallocStackLoggingNoCompact = 1。在控制台中,我看到
2010-03-01 19:13:46.924 CruzNomad[7952:207] *** -[CFString stringByAddingPercentEscapesUsingEncoding:]: message sent to deallocated instance 0x58448e0
2010-03-01 19:13:46.924 CruzNomad[7952:207] *** -[CFString stringByAddingPercentEscapesUsingEncoding:]:消息发送到释放实例 0x58448e0
Then at the (gdb) prompt, I did info malloc-history 0x58448e0, which gave me:
然后在 (gdb) 提示符下,我做了info malloc-history 0x58448e0,这给了我:
Alloc: Block address: 0x058448e0 length: 64
Stack - pthread: 0xa0b33500 number of frames: 25
0: 0x98e089bc in malloc_zone_malloc
1: 0x21516aa in _CFRuntimeCreateInstance
2: 0x2152bf8 in __CFStringCreateImmutableFunnel3
3: 0x21567d9 in CFStringCreateCopy
4: 0x21742fc in _CFStringCreateWithFormatAndArgumentsAux
5: 0xdb546 in -[NSPlaceholderString initWithFormat:locale:arguments:]
6: 0xdb4d8 in +[NSString stringWithFormat:]
7: 0x23aa3 in -[BuisnessCardViewController viewDidLoad] at /Users/.../Classes/BuisnessCardViewController.m:85
8: 0x3d6796 in -[UIViewController view]
9: 0x347b4 in -[gm_menuViewController btn5_Pressed:] at /Users/.../Classes/menuViewController.m:535
10: 0x357459 in -[UIApplication sendAction:to:from:forEvent:]
11: 0x3baba2 in -[UIControl sendAction:to:forEvent:]
12: 0x3bcdc3 in -[UIControl(Internal) _sendActionsForEvents:withEvent:]
13: 0x3bbb0f in -[UIControl touchesEnded:withEvent:]
14: 0x370e33 in -[UIWindow _sendTouchesForEvent:]
15: 0x35a81c in -[UIApplication sendEvent:]
16: 0x3610b5 in _UIApplicationHandleEvent
17: 0x2984ed1 in PurpleEventCallback
18: 0x2197b80 in CFRunLoopRunSpecific
19: 0x2196c48 in CFRunLoopRunInMode
20: 0x298378d in GSEventRunModal
21: 0x2983852 in GSEventRun
22: 0x362003 in UIApplicationMain
23: 0x2c8c in main at /Users/.../source/main.m:14
24: 0x2bfa in start
Line 7 says the problem was in line 85 of BuisnessCardViewController.m. That line is here:
第 7 行表示问题出在 BuisnessCardViewController.m 的第 85 行。那条线在这里:
fullAddress = [NSString stringWithFormat:@"%@ %@", fullAddress, myString];
I'm appending the contents of fullAddressand myStringand storing it back in fullAddress.
我正在追加fullAddress和myString的内容并将其存储回fullAddress。
If I'm interpreting this correctly, it appears that after this line, fullAddressis deallocated. When I drop a breakpoint and hover over the variable, its value says "out of scope."
如果我正确地解释了这一点,似乎在这一行之后,fullAddress被释放了。当我放下断点并将鼠标悬停在变量上时,它的值显示“超出范围”。
fullAddressworks later in this method. I use it to send to send to Google for reverse geocoding in line 164 of the same method.
fullAddress稍后在此方法中起作用。我用它发送到谷歌以在相同方法的第 164 行进行反向地理编码。
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", [fullAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Even here, it says "out of scope." I'm stumped...any advice??
即使在这里,它也说“超出范围”。我很难过……有什么建议吗??
Thanks!
谢谢!
Thomas
托马斯
采纳答案by mxg
In most cases this happens when you don't retain an attribute which is object and send late messages to it in other methods, late.
在大多数情况下,当您不保留作为对象的属性并在其他方法中向它发送延迟消息时,会发生这种情况。
So, where some strings are initialized try:
因此,在初始化某些字符串的地方尝试:
[fullAddress retain];
or
或者
[myString retain];
depending which one is initialized in other method.
取决于哪个是在其他方法中初始化的。
回答by stefanB
Have you tried appending string with format?
你试过用格式附加字符串吗?
fullAddress = [NSString stringWithFormat:@"%@ %@", fullAddress, myString];
with:
和:
- (NSString *)stringByAppendingFormat:(NSString *)format ...
like this:
像这样:
[fullAddress stringByAppendingFormat:@" %@", myString];