I use NSString to append strings, but when I release the “cacheStr” NSString, the iphone simulator crash.
Where should I put the release code ?
init code : I use three NSString to append content of the 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];
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...
[dataArray release]; [notiyStr release]; // [cacheStr release]; /* crash ... */Thanks!
its more simple to declare a NSString like this
NSString *tmpStr = @"This is a test message!";then no need to release it,when you dont use alloc