当我尝试释放 nsmutablestring 时应用程序崩溃,请帮忙解决这个问题

发布于 2024-11-24 03:42:31 字数 1273 浏览 4 评论 0原文

初始化的 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

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒数 2024-12-01 03:42:34

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

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

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

话虽如此,您声称程序在到达“dealloc”时崩溃。这意味着 temp1 在您的代码中被声明为实例变量...如果是这种情况,正确的代码应该是(假设 temp1 是声明的属性设置了保留属性):

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

如果 temp1 不是实例变量也不是属性,您可能希望在方法内部指示 temp1 是 NSMutableString 并返回自动释放的对象。

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.

雨落星ぅ辰 2024-12-01 03:42:33

您可以将可变字符串作为自动释放返回

,或者

参考 this.. .

you can return your Mutable string as auto release

OR

refer this...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文