当我尝试释放 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
提前致谢
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:@""];
NSRange range = [temp1 rangeOfString:@""];
NSRange range1 = NSMakeRange(range.location, 8);
if (range1.location != NSNotFound) {
NSString* temp2 = [temp1 stringByReplacingCharactersInRange:range1 withString:@""];
//[temp1 setString:temp2];
temp1 = temp2;
range = [temp1 rangeOfString:@""];
while (range.location < [temp1 length]) {
range1 = NSMakeRange(range.location, 8);
temp2 = [temp1 stringByReplacingCharactersInRange:range1 withString:@""];
//[temp1 setString:temp2];
temp1 = temp2;
range = [temp1 rangeOfString:@""];
}
}
//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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您在方法内进行此调用。根据您提供的代码,确保代码片段实际上是:
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.
您可以将可变字符串作为自动释放返回
,或者
参考 this.. .
you can return your Mutable string as auto release
OR
refer this...