views:

33

answers:

2

Hey guys, lately I have been asking quite a few questions about memory management on the iPhone. Fortunately things are getting clearer. But I still struggle when it gets more complex: So is there something wrong with this in terms of memory mangement? My question and suggestions are in the comments...

//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;
}
+1  A: 

Your code looks correct memory-management-wise. Just remember, if you don't have call a method with alloc, new, retain, or copy in the method name, you don't have to worry about releasing.

One small point--your first 3 lines of code are redundant and inefficient. You shouldn't usually use initWithString--copy is usually a better choice when dealing with immutable objects, since behind the scenes a copy method can be replaced by a (less expensive) retain method. In your case, you don't even need to use copy--[self replaceDynamicRegex: txtField.text] will have the same result. Likewise, instead of [NSString stringWithString:[html stringByMatching:theRegex]], you can use simply use [html stringByMatching:theRegex] (since that method returns a new string).

Another note--html = (NSMutableString*)[html stringByReplacingOccurrencesOfRegex:currentTag withString:replacementTag] is incorrect. stringByReplacingOccurrencesOfRegex: returns an NSString, which can't be cast to an NSMutableString (you'll likely get a crash later on when you send a mutating method to the string). Instead, use [[html stringByReplacingOccurrencesOfRegex:currentTag withString:replacementTag] mutableCopy]

eman
Allright thanks. But how would I "release" the mutableCopy then? With an autorelease: return [html autorelease]; ?!?
jagse
@jagse: Yes, you'd want to autorelease it if you copy it.
eman
A: 

Generally, when you see a method named xWithY, you can assume the string will be autorelease-d.

Therefore, you probably do not need to autorelease the value returned from -stringByReplacingOccurrencesOfRegex:withString:.

The rest of your code looks okay, to me.

Alex Reynolds