Mutablecopy mem泄漏?

时间:2013-08-20 10:30:26

标签: iphone

这行会泄漏内存吗?请解释是或否。

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方法上发布它。

1 个答案:

答案 0 :(得分: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)];