In my Function below if I remove the tempString release statement it works just fine but with it, there is ALWAYS a runtime error. It is a simple function that displays an array in an NSTextField either _stackDisp1 or _stackDisp2 but for some reason releasing the string creates a runtime error Any help?
- (void) displayArr:(NSMutableArray*)stack{
NSTextField *myObj;
if([stack count] <= 10) myObj = _stackDisp1;
else myObj = _stackDisp2;
NSString *tempString = [[NSString alloc]initWithString:@""];
for(NSString *i in stack){
tempString = [NSString stringWithFormat:@"%@\n%@",tempString,i];
}
[myObj setStringValue:tempString];
[tempString release];
}
That’s because
creates a new autoreleased object assigning it to your variable
tempString. The pointer to the first object gets lost and you end up over-releasing an autoreleased object. Just change the initial assignment toand remove the
[tempString release]line.