app is crashing when i try to release nsmutablestring please help on this
intialised NSMutableString
as below:
-(NSString*)filterIt:(NSString*)source
{
temp1= [[NSString alloc] initWithString:[source stringByReplacingOccurrencesOfString:@"rlm;" withString:@""]];
//NSString *m_temp;
temp1 = [temp1 stringByReplacingOccurrencesOfString:@"&" withString:@""];
temp1 = [temp1 stringByReplacingOccurrencesOfString:@"#x" withString:@"&#开发者_运维技巧x"];
NSRange range = [temp1 rangeOfString:@"&#x"];
NSRange range1 = NSMakeRange(range.location, 8);
if (range1.location != NSNotFound) {
NSString* temp2 = [temp1 stringByReplacingCharactersInRange:range1 withString:@""];
//[temp1 setString:temp2];
temp1 = temp2;
range = [temp1 rangeOfString:@"&#x"];
while (range.location < [temp1 length]) {
range1 = NSMakeRange(range.location, 8);
temp2 = [temp1 stringByReplacingCharactersInRange:range1 withString:@""];
//[temp1 setString:temp2];
temp1 = temp2;
range = [temp1 rangeOfString:@"&#x"];
}
}
//m_temp = [temp1 mutableCopy];
// [temp1 release];
return temp1;
}
if i try to release this string in dealloc method and try to run the app my app is crashing.
please give me some suggestions that how can i release this temp1
Thanks in advance
you can return your Mutable string as auto release
OR
refer this...
I'm assuming you are making this call inside a method. Based on the code you provided, make sure the code fragment is actually:
temp1= [[NSMutableString alloc]
initWithString:[source stringByReplacingOccurrencesOfString:@"rlm;" withString:@""]];
I'm assuming you are calling stringByReplacingOcurrenceOfString:withString: to sources.
Having said that, you claim that the program crashes when reaching 'dealloc'.. this would mean that temp1 is declared as an instance variable in your code... If that is the case, the correct code should be (assuming temp1 is a declared property with retain attribute set):
self.temp1 = [[NSMutableString alloc]
initWithString:[source stringByReplacingOccurrencesOfString:@"rlm;" withString:@""]];
If temp1 is not an instance variable nor a property, you might want to indicate inside the method that temp1 is a NSMutableString and return the object autoreleased.
精彩评论