NSArray发布崩溃应用程序

时间:2011-11-04 08:23:16

标签: objective-c ios memory-management

我正在尝试“标记”我从文本文件中获取的数据。

当我这样做时,我收到这样的错误:

  

malloc: *对象0x844c730的错误:未分配被释放的指针   * 在malloc_error_break中设置断点以进行调试

我使用的代码如下所示:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"mydata" ofType:@"txt"];
NSString *rawText = [NSString stringWithContentsOfFile:filePath encoding:NSASCIIStringEncoding error:nil];
//No error was caused by above line
NSArray *tmp = [rawText componentsSeparatedByString:@"##@@"];
NSString *title = @"";
NSString *detail = @"";
for(int i = 0; i < [tmp count]-1; i++)
{
    NSArray *base = [[tmp objectAtIndex:i] componentsSeparatedByString:@"##"];
    title = [[NSString alloc] initWithFormat:@"%@$$%@",title,[base objectAtIndex:0]];
    detail = [[NSString alloc] initWithFormat:@"%@$$%@ | %@ | %@",
              title,
              [base objectAtIndex:0],
              [base objectAtIndex:1],
              [base objectAtIndex:2]
              ];
    [base release];
}
[tmp release];

它必须是代码的这一部分,因为如果我评论这篇文章,它就可以正常工作。

阅读错误set a breakpoint,我不知道将其放在malloc_error_break

我的记忆管理有什么问题?

或者我怎样才能以其他方式拆分字符串?

4 个答案:

答案 0 :(得分:3)

您从tmp获得componentsSeparatedByString:。由于该选择器不以“alloc”或“new”或“copy”或“mutableCopy”开头,并且由于您没有[tmp retain],因此您不拥有tmp。所以你不应该[tmp release]

base相同。

答案 1 :(得分:1)

Base和temp是autorelease个对象,所以你不应该释放那些对象。

答案 2 :(得分:0)

您不必释放base。它已经自动释放了。

答案 3 :(得分:-1)

您正在尝试释放阵列基础而不分配它。 NSArray * base = [[tmp objectAtIndex:i] componentsSeparatedByString:@“##”];没有为base分配内存。你不需要[base release];  直到它被分配。

相关问题