我用下面的代码:
for (int i=1; i<=3;i++){
NSString *theString = [[NSString alloc] initWithFormat:@"%i",imageValue];
theString = [theString stringByAppendingFormat:@"%i.jpg",i];
[img addObject:theString];
NSLog(@"the string %@:",theString); //01.jpg ,02.jpg and 03.jpg
[theString release];
}
但是我收到此错误3次,为什么?
错误:
myapp(15467,0xacdaa2c0) malloc: *** error for object 0x4eb1ca0: pointer being freed was not allocated
最佳答案
stringByAppendingFormat返回一个新字符串,该字符串会自动释放。因此,这意味着您要释放一个自动释放的对象,这就是为什么您会收到错误消息,并且泄漏了分配给第一行的字符串的原因。我建议将第一行更改为
NSString* theString = [NSString stringWithFormat:@"%i", imageValue];
然后将其完全删除。