在测试一些代码时,我遇到了一个问题。听到是我的代码和日志。NSString释放问题
(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对象的实例已被释放!
如果我发送消息给接收者发布两次,第一个位置在哪里?
谁能告诉我它在哪里?
谢谢。
谢谢,jok! :D – leechoohyoung 2011-06-03 06:48:16