我一直在使用以下代码,几乎没有任何问题,但今天 Xcode 开始抱怨它 BIGTIME。在我发现错误/内存泄漏确实是之前,我几乎发疯了。Xcode 说“发送到已释放实例的消息”。我评论了几行(版本),它停止了崩溃。
问题是,我在这里完全迷失了......我不应该释放那些以避免内存泄漏吗?我为这个项目瞄准 iOS 4.0,所以手动内存管理是必须的。
+ (NSString*)reformatRule:(NSString*) ruleStr {
NSString *bufRule = [[[NSString alloc] init] autorelease];
NSString *buf = [[NSString alloc] init];
char c = 0;
NSString *value = [[NSString alloc] init];
for(int i=0;i<[ruleStr length];i++){
c = [ruleStr characterAtIndex:i];
if([self isCharacterOrDigit:[NSString stringWithFormat:@"%c",c]]){
buf = [buf stringByAppendingString:[NSString stringWithFormat:@"%c",c]];
}else{
DLog(@"says:%@",buf);
value = [buf stringByReplacingOccurrencesOfString:@" " withString:@""];
//[buf release];
//buf = [[NSString alloc] init];
buf = @"";
if (![value isEqualToString:@""]) {
if ([value isEqualToString:CONDITION_AND] || [value isEqualToString:CONDITION_OR]) {
bufRule = [bufRule stringByAppendingString:[NSString stringWithFormat:@"%@",value]];
}else{
bufRule = [bufRule stringByAppendingString:[NSString stringWithFormat:@"(%@)",value]];
}
}
if(c!=' '){
bufRule = [bufRule stringByAppendingString:[NSString stringWithFormat:@"%c",c]];
}
}
}
// [value release]; COMMENTED THIS LINE
// [buf release]; AND THIS ONE TOO
return bufRule;
}