【发布时间】:2011-06-03 06:03:30
【问题描述】:
在测试一些代码时,我遇到了一个问题。听到的是我的代码和日志。
(IBAction) DynamicBtnClicked:(id)sender
{
NSString *strLog = [[NSString alloc] initWithString:@"SubView Index is..."];
NSLog(@"initialized strLog address is = %p, retainCount = %d", strLog, [strLog retainCount]);
if ([self.view.subviews count] > 0) {
for (int i = 0 ; i < [self.view.subviews count] ; i++) {
UIView *tmpView = [self.view.subviews objectAtIndex:i];
strLog = [strLog stringByAppendingFormat:@"%d view`s index = %d, tag = %d", i, i,[tmpView tag]];
NSLog(@"appended strLog address is = %p, retainCount = %d", strLog, [strLog retainCount]);
}
}
NSLog(@"after appended strLog address is = %p, retainCount = %d", strLog, [strLog retainCount]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:strLog delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
NSLog(@"after using strLog address is = %p, retainCount = %d", strLog, [strLog retainCount]);
[strLog release];
}
下面是日志
2011-06-03 14:36:11.038 MakeViewUsingCode[3918:40b] initialized strLog address is = 0x45c4, retainCount = 2147483647
2011-06-03 14:36:11.039 MakeViewUsingCode[3918:40b] appended strLog address is = 0x9c028d0, retainCount = 1
2011-06-03 14:36:11.040 MakeViewUsingCode[3918:40b] appended strLog address is = 0x9c021b0, retainCount = 1
2011-06-03 14:36:11.041 MakeViewUsingCode[3918:40b] after appended strLog address is = 0x9c021b0, retainCount = 1
2011-06-03 14:36:11.081 MakeViewUsingCode[3918:40b] after using strLog address is = 0x9c021b0, retainCount = 3
2011-06-03 14:36:11.087 MakeViewUsingCode[3918:40b] *** -[CFString release]: message sent to deallocated instance 0x9c021b0
dlopen(/Developer/Library/Xcode/PrivatePlugIns/DebuggerFoundation.ideplugin/Contents/Resources/DebuggerIntrospectionSupport.dylib, 0x0000000A)
dyld: loaded: /Developer/Library/Xcode/PrivatePlugIns/DebuggerFoundation.ideplugin/Contents/Resources/DebuggerIntrospectionSupport.dylib
Current language: auto; currently objective-c
我知道,我的代码中存在内存泄漏,但这不是真正的问题。(这是故意的。)
真正的问题是,当我运行我的代码时,会发生 EXC_BAD_ACCESS 运行时错误。
在我的代码中,我向接收者发送了一条消息以释放一次,但日志显示错误的原因是 NSString 对象的实例已被释放!
如果我向接收者发送消息两次释放,第一个位置在哪里?
谁能告诉我它在哪里?
谢谢。
【问题讨论】:
标签: ios nsstring release dealloc