0

这条线会泄漏内存吗?请说明是或否。

Person.h
-------
NSMutableString *address;


Person.m
--------
@synthesize address;

-(id) init
{
    self = [super init];
    address = [[NSMutableString alloc] init];

    return self;
}

-(void) funcA()
{
    [address appendFormat:@"located|at|%@", @"Singapore"];
     address = [[address stringByReplacingOccurrencesOfString:@"|" withString:@" "] mutableCopy];
}

-(void) dealloc
{
    [address release];
    [super release]
}

=address 是一个 NSMutableString,它也是一个属性。我只在 dealloc 方法上释放它。

4

1 回答 1

0

你在这里泄漏:

[address appendFormat:@"located|at|%@", @"Singapore"];
address = [[address stringByReplacingOccurrencesOfString:@"|" withString:@" "] mutableCopy];

您将NSMutableString在第二行用新地址替换地址。

只需使用replaceOccurrencesOfString:withString:options:range:,因此您不必替换字符串。

[address appendFormat:@"located|at|%@", @"Singapore"];
[address replaceOccurrencesOfString:@"|" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, address.length -1)];
于 2013-08-20T10:34:02.397 回答