iphone - App 在 Instrument 中显示泄漏,这个简单的代码有什么问题?

标签 iphone objective-c ios ios5 instruments

我很简单地将我的数据导出到 CSV 文件中。但是当我点击导出按钮时,Leak Instrument 显示 [NSPlaceholderMutableString init] 错误。

我没有在我的 NSMutableString 中的任何地方使用 Placeholder,那么 Instrument 显示 Leak 是怎么回事。

这是我的代码:

- (IBAction)btnExportDataPressed:(id)sender
{
    csvString = [[NSMutableString alloc] init];
    csvString = [NSMutableString stringWithFormat:@"No.,Name,Type,MaskPAN,SwipeTime\n"];
    NSString *msg = @"";

    if (self.totalCards.count>0)
    {
        for (int i=0; i<self.totalCards.count; i++)
        {
            self.cardInfo = [self.totalCards objectAtIndex:i];
            [csvString appendString:[NSMutableString stringWithFormat:@"%d,%@ %@,%@,%@,%@\n",i+1,self.cardInfo.firstName,self.cardInfo.lastName,self.cardInfo.type,self.cardInfo.maskedPAN,self.cardInfo.swipeTime]];
        }
        NSLog(@"csvString:%@",csvString);

        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *fileName = [NSString stringWithFormat:@"ORANGE_BOWL_%@",[NSDate date]];

        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.csv",fileName]];
        [fileManager createFileAtPath:fullPath contents:[csvString dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
        msg = @"Data exported successfully. Connect the device to iTunes to get the records.";
    }
    else
    {
        msg = @"No Data to Export";
    }

    [csvString release];        
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Discover Orange Bowl," message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}

下面是仪器的错误图片

enter image description here

最佳答案

您的代码顶部存在内存泄漏:

csvString = [[NSMutableString alloc] init];
csvString = [NSMutableString stringWithFormat:@"No.,Name,Type,MaskPAN,SwipeTime\n"];

因此,您正在为一个可变字符串(第 1 行)分配内存,这会使保留计数增加 1,然后在第 2 行中,您丢失了指向它的指针引用,因为您将另一个字符串分配给了该指针。这肯定会泄漏内存;不确定您的其余代码是否在做其他不好的事情。

所以删除第一行——它没有用,只会导致内存泄漏。

此外,您的 [csvString release] 行看起来不对,因为当您上次将某些内容分配给 csvstring 时,您使用了 [NSMutableString stringWithFormat] 而它不会不要增加保留计数(因此您不应该使用 release 来减少它)。因此,请尝试删除显示 [csvString release]; 的行。

Leaks 工具可以显示对您实际上没有使用的对象的稍微令人费解的引用;这是由于这些对象被您使用过的对象在内部使用(和泄漏)造成的。

关于iphone - App 在 Instrument 中显示泄漏,这个简单的代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8376637/

相关文章:

ios - 使用 iOS 委托(delegate)

ios - 如何从 uint8_t 数组获取 "remove"数组元素或 ge 范围?

iPhone SDK : UISearchBar: searchBarTextDidEndEditing not firing

ios - 如何从详细 View Controller 中的选定单元格获取 cell.textLabel.text?

iphone - 在 AlertView OK 按钮声明中执行一段代码

objective-c - 为什么 "conformsToProtocol"不检查 "required"方法实现?

ios - 发布带链接的 Facebook 照片

objective-c - 如何更改 ViewController 之间的 segue 速度

iphone - 在其他方法中引用以编程方式创建的对象

ios - 如何在 Swift 中显示当前 PDF 页码并将该页码保存为书签?