分配时内存泄漏

时间:2016-05-17 02:05:14

标签: ios objective-c swift memory-management instruments

enter image description here

我是初学者,仍在努力弄清楚如何阅读本文。 有3个自定义视图,在开始时我分配第一个。 然后第二个/解除分配第一个然后第三个/解除分配第二个。

我在释放每个视图之前就清空所有数组,所以从我看到的,每次分配视图时保留的所有内存都应该在每次释放/ nil时释放,但是在图中它会不断增加,我不会看到任何被释放的东西。

这应该是那样的吗?我没有代表,数组,字典等一切。

-(void)firstTOsecond {

[self.first removeFromSuperview];
self.first.delegate = nil;
self.first = nil;


self.second = [[Second alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.second];
self.second.delegate = self;}

-(void)secondTOthird {

[self.second removeFromSuperview];
self.second.delegate = nil;
self.second = nil;


self.third = [[Third alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.third];
self.third.delegate = self;}

修改

在First.m / Second.m / Third.m

-(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
    self.buttonStartFrame = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"buttonStartFrame"]];
    [self addSubview:self.buttonStartFrame];
    self.buttonStartFrame.userInteractionEnabled = YES;
    self.buttonStartButton = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"buttonStartDefault"]];
    [self.buttonStartFrame addSubview:self.buttonStartButton];
    self.buttonStartButton.userInteractionEnabled = YES;
    self.labelStart = [[UILabel alloc]init];
    [self.buttonStartButton addSubview:self.labelStart];
    self.labelStart.textColor = [UIColor darkGrayColor];
    self.labelStart.text = @"Start";
    }
return self;}

1 个答案:

答案 0 :(得分:0)

内存泄漏可能是你在不断更新时不断创建新的视图元素。你可以创建帮助方法以避免分配新视图,而是更新主线程上的视图

- (void)limitedUpdateTextColorView:(NSDictionary *)somethingCache {
    self.labelStart.textColor = [somethingCache valueForKey:@"bar"];
    self.labelStart.text = [somethingCache valueForKey:@"foo"];
}