发布时NSString崩溃

时间:2012-03-30 11:00:12

标签: iphone crash nsstring release

我使用NSString追加字符串,但是当我发布“cacheStr”NSString时,iphone模拟器崩溃了。 我应该把发布代码放在哪里?

init代码:我使用三个NSString来附加dataArray的内容。

NSMutableArray *dataArray = [[NSMutableArray alloc] init];
NSString *cacheStr = [[NSString alloc] init];
NSString *tmpStr = [[NSString alloc] init];
NSString *notiyStr = [[NSString alloc] initWithFormat:@"This is a test message!"];
dataArray = [NSArray arrayWithObjects:
                      @"currency1.png",
                      @"currency2.png",
                      @"currency3.png",
                      @"currency4.png",
                      @"currency5.png",
                      @"xxxxxx",
                      @"currency1.png",
                      @"currency2.png",
                      @"currency3.png",
                      @"currency4.png",
                      @"currency5.png",
                      @"xxxxxx",
                      nil];

NSMutableArray *dataArray = [[NSMutableArray alloc] init]; NSString *cacheStr = [[NSString alloc] init]; NSString *tmpStr = [[NSString alloc] init]; NSString *notiyStr = [[NSString alloc] initWithFormat:@"This is a test message!"]; dataArray = [NSArray arrayWithObjects: @"currency1.png", @"currency2.png", @"currency3.png", @"currency4.png", @"currency5.png", @"xxxxxx", @"currency1.png", @"currency2.png", @"currency3.png", @"currency4.png", @"currency5.png", @"xxxxxx", nil];

append string code : use for loop to append strings.

int isFailed = 0;
int countOfDataArray = [dataArray count];
if (!isFailed) {
    for (int i=0; i < countOfDataArray; i++) {
        if ([[dataArray objectAtIndex:i] isEqualToString:@"xxxxxx"]) {
            tmpStr = [cacheStr stringByAppendingFormat:@"%@\n", [dataArray objectAtIndex:i]];
        }
        else {
            tmpStr = [cacheStr stringByAppendingFormat:@"value %d : %@\n", i+1, [dataArray objectAtIndex:i]];
        }
        cacheStr = [tmpStr copy];
        [tmpStr release];
    }
}
tmpStr = [notiyStr stringByAppendingString:cacheStr];

release code : when I add [cacheStr release], the simulator will crash...

append string code : use for loop to append strings.

谢谢!

3 个答案:

答案 0 :(得分:2)

声明像这样的NSString更简单
NSString *tmpStr = @"This is a test message!";
当你不使用alloc

时,不需要释放它

答案 1 :(得分:1)

这个

NSMutableArray *dataArray = [[NSMutableArray alloc] init];
dataArray = [NSArray arrayWithObjects:
                  @"currency1.png",
                  @"currency2.png",
                  @"currency3.png",
                  @"currency4.png",
                  @"currency5.png",
                  @"xxxxxx",
                  @"currency1.png",
                  @"currency2.png",
                  @"currency3.png",
                  @"currency4.png",
                  @"currency5.png",
                  @"xxxxxx",
                  nil];

很奇怪!您正在分配一个可变数组,并将指向该数组的指针分配给您不拥有的数组。当您尝试使用它时,它可能已经(自动)发布了。因此int countOfDataArray = [dataArray count];会导致countOfDataArray为零。因此永远不会设置cacheStr。仍然不应该在释放cacheStr时崩溃。

将其更改为:

NSMutableArray *dataArray = [[NSMutableArray alloc] initWithObjects: @"currency1.png",
                  @"currency2.png",
                  @"currency3.png",
                  @"currency4.png",
                  @"currency5.png",
                  @"xxxxxx",
                  @"currency1.png",
                  @"currency2.png",
                  @"currency3.png",
                  @"currency4.png",
                  @"currency5.png",
                  @"xxxxxx",
                  nil];

你应该没事。

答案 2 :(得分:0)

不要使用[NSString alloc] init。使用NSString值= @&#34;消息&#34 ;;无需释放它。