2012-07-13 34 views
0

我在看内存中的堆镜头。这个功能似乎是废弃记忆的罪魁祸首。这是造成遗弃的记忆?

它是我的一个视图'MyView'的视图构建代码的一部分。

如果我用这个函数创建并销毁'MyView'100次,那么注释的内存大小总是返回到它的基线。但是,如果我在我的记忆中留下这个功能不断增加。

据我所见,我不会取得该功能中的任何东西的所有权。 我在做什么错?

-(void)drawPointAroundCircle:(CGPoint)centerpoint radius:(int)radius amount:(int)amount 
{ 


    CGPoint pointarray[amount]; 
    float thetaarray[7]={270.0,315.0,0.0,45.0,135,180.0,225.0}; 
    int i=-1; 
    UIGraphicsBeginImageContext(CGSizeMake(self.frame.size.width,self.frame.size.height)); 

    CGContextRef context=UIGraphicsGetCurrentContext(); 
    for (i=0; i<7; i++) { 




     float x=cosf(D2R(thetaarray[i]))*radius; 
     float y =sinf(D2R(thetaarray[i]))*radius; 
     x=x+centerpoint.x; 
     y=y+centerpoint.y; 

     pointarray[i].x=x; 
     pointarray[i].y=y; 

     UIBezierPath *path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(x, y) radius:2 startAngle:D2R(0) endAngle:D2R(360) clockwise:YES]; 

     CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);  


     [path fill]; 
     [path closePath]; 
     } 



    UIImage *bezierimage=UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    UIImageView *bezierImageView=[[UIImageView alloc]initWithImage:bezierimage]; 

    [self addSubview:bezierImageView]; 

} 
+0

您是否曾经再次删除子视图或者继续添加一个子视图?在该方法中,您创建bezierviewImage,addSubView将保留它。 ReleaseFromSuperview会根据内存分配释放它(意思是将保留计数减1)。 – 2012-07-13 14:21:53

+0

Bah !!!我的项目中99%使用弧线。 'MyView'是没有的1%。我从未称呼释放。谢谢 – dubbeat 2012-07-13 14:32:10

回答

1

bezierImageView应该在将它添加到子视图之前自动释放,或者在将其添加到子视图后释放。使用您当前的代码,该对象的保留计数不会低于1。

这是假设非ARC,但如果您使用ARC,它不会泄漏。

+0

在我的问题下面看到我的尴尬评论。谢谢 – dubbeat 2012-07-13 14:33:42

+0

我的项目中1%使用ARC,所以我认为它没有开启,这也可能令人尴尬。 – Mark 2012-07-13 14:35:05

0

你,其实,“取得所有权”的东西这条线:

UIImageView *bezierImageView=[[UIImageView alloc]initWithImage:bezierimage]; 

如果你不使用自动引用计数(ARC),你应该在方法结束前自动释放bezierImageView 。