嘿,最近我一直在問iPhone的內存管理問題。幸運的是事情變得更加清晰。但是,當它變得更加複雜時,我仍然很掙扎:那麼就存儲器管理而言,這有什麼問題嗎?我的問題和建議在評論中...iPhone,客觀的c重新分配和返回方法的指針
//I get a text from a textfield
NSString *text = [[NSString alloc]initWithString:txtField.text];
NSMutableString *newText = [self replaceDynamicRegex:text];
[text release];
...
//The method replaces regex it finds in the text. The regex part is just pseudo code
//and I just interested in memory management
-(NSMutableString*)replaceDynamicRegex:(NSString*)txt{
NSString *currentTag = [NSString stringWithString:@"dynamiclyCreatedTag"];
//As long as we find a particuar regex (just pseuo code here) we replace it
while (currentTag != NULL) {
if([html stringByMatching:openingTag] == NULL){
break;
}
//regular expression
currentTag = [NSString stringWithString:[html stringByMatching:theRegex]];
//Get rid of the useless part of the currentTag pseudo code
NSString *uselessTagPart = @"uselessRegex";
//Reassignment of the pointer currentTag --> ok to do this? cause I did not alloc]init]?
//and instead used stringWithString wich then gets autoreleased
currentTag = [currentTag stringByReplacingOccurrencesOfRegex:uselessTagPart withString:@""];
//Reassignment of the pointer html --> Ok to do this? cause it is just a pointer and the
//object is being released after the method call (further up)
html = (NSMutableString*)[html stringByReplacingOccurrencesOfRegex:currentTag withString:replacementTag];
}
//Do I need to autorelease this?
return html;
}
好的謝謝。但是,我將如何「釋放」mutableCopy呢?用autorelease:return [html autorelease]; ?!? – jagse 2010-07-25 19:42:01
@jagse:是的,如果你複製它,你會想自動釋放它。 – shosti 2010-07-25 21:56:47