【发布时间】:2023-03-24 21:57:01
【问题描述】:
我将 xcode 4.2 用于 10.6 和 ios 5,当我使用 xcode 分析项目时报告了这个奇怪的内存泄漏。
代码如下:
- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info
{
id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for (symbol in results) break;
[reader dismissModalViewControllerAnimated: NO];
[self beep];
NSString *upcScanned = [NSString stringWithFormat:@"0%@", symbol.data]; //line 1
NSString * name = [self getItemName:upcScanned];
ProductNameDialog *dialog = [[ProductNameDialog alloc] initWithNibName:@"ProductNameDialog" bundle:nil];
//dialog.upcScanned = [upcScanned substringToIndex:[upcScanned length] - 1];//line 2
[name release];
[self presentModalViewController:dialog animated:YES];
[dialog release];
}
line2 是报告内存泄漏的行。
【问题讨论】:
-
添加断点或者登录对话框发布看看是否真的被调用了。
-
您是否也在对话框的
dealloc方法中释放upcScanned(或将其设置为nil,因为您尚未定义实例变量)?viewDidUnload不一定会被调用,它通常只在内存不足的情况下使用。 -
泄漏检测器报告分配泄漏对象的行。静态分配的字符串永远不会被视为泄漏。您可能在其他一些方法中使用 upcScanned,但保留不平衡。