下面的代码出现了漏洞!我为什么有这个问题?是因为它复制了NSString中的属性。有办法绕道吗?
@property (nonatomic, copy) NSString *reg;
@property (nonatomic, copy) NSString *reg2;
@property (nonatomic, copy) NSNumber *altitude;
@property (nonatomic, copy) NSNumber *heading;
-(void)updateTitles{
self.title=[NSString stringWithFormat:@"%@ %@",self.reg,self.reg2];
self.subtitle = [NSString stringWithFormat:@"%@ft %@°",self.altitude,self.heading];
}此方法中每个属性设置的泄漏率为50%。
更新
最终发现这是从一个街区打来的。为了努力解决这个问题,我做了以下工作。
以下的工作,但仍然泄漏,清除现在自我保留。
-(void)updateTitles{
__block NSString *thisTitle = [[NSString alloc] initWithFormat:@"%@ %@",self.reg1,self.reg2];
__block NSString *subTitle = [[NSString alloc] initWithFormat:@"%@ft %@°",self.altitude,self.heading];
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue,^(){
self.title=thisTitle;
self.subtitle = subTitle;
[thisTitle release];
[subTitle release];
});
}但是,这一漏洞以及理论上应该给出的setTitle方法的未识别选择器!
-(void)updateTitles{
__block NSString *thisTitle = [[NSString alloc] initWithFormat:@"%@ %@",self.reg1,self.reg2];
__block NSString *subTitle = [[NSString alloc] initWithFormat:@"%@ft %@°",self.altitude,self.heading];
__block __typeof__(self) blockSelf = self;
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue,^(){
[blockSelf setTitle:thisTitle];
[blockSelf setSubtitle:subTitle];
[thisTitle release];
[subTitle release];
});
}发布于 2011-12-14 10:23:01
假设您没有使用ARC,具有上述属性的对象是否有一个dealloc方法,并且它是否正确地释放了ivars?该对象本身是否由保存它的任何对象释放?
重写getter而不是设置title/subtitle是否会产生任何不同:
-(NSString *) title
{
return [NSString stringWithFormat:@"%@ %@",self.reg,self.reg2];
}等。
https://stackoverflow.com/questions/8502738
复制相似问题