目标c -应用程序崩溃时,我试图释放nsmutablestring请帮助这一点



初始化NSMutableString如下:

 -(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;
}

如果我尝试在dealloc方法中释放这个字符串并尝试运行应用程序,我的应用程序会崩溃。

请给我一些建议,我如何才能释放这个temp1

Thanks in advance

你可以返回你的可变字符串作为自动释放

引用这个…

我假设你是在一个方法内部进行这个调用。根据您提供的代码,确保代码片段实际上是:

temp1= [[NSMutableString alloc] initWithString:[source stringByReplacingOccurrencesOfString:@"rlm;" withString:@""]];

我假设你正在调用stringByReplacingOcurrenceOfString:withString:到源。

说了这么多,你声称程序在到达'dealloc'时崩溃…这意味着在代码中将temp1声明为实例变量…如果是这种情况,正确的代码应该是(假设temp1是带有retain属性集的声明属性):

self.temp1 = [[NSMutableString alloc] initWithString:[source stringByReplacingOccurrencesOfString:@"rlm;" withString:@""]];

如果temp1不是实例变量,也不是属性,你可能想在方法中指出temp1是一个NSMutableString,并返回对象自动释放。

最新更新