如何在另一个函数中创建dealloc对象?

时间:2014-05-19 19:02:52

标签: ios objective-c memory-management ios7 automatic-ref-counting

我有 UICollisionBehavior 和一些边界( addBoundaryWithIdentifier:fromPoint:toPoint: ):在屏幕的左侧,右侧和底部。

我这样做(只是下降的块,像下雨):

- (void) movingBlocks {
    UIView *block = [[UIView alloc] initWithFrame:
                     CGRectMake(arc4random() % 320, -20, 20, 20)];
    [block setBackgroundColor: [UIColor brownColor]];
    [self.view addSubview: block];
    [blockCollision addItem: block];
    [pushBlock addItem: block];
    [self performSelector:@selector(movingBlocks)
               withObject:nil
               afterDelay:0.5];
}

然后,当碰撞发生时:

- (void)collisionBehavior:(UICollisionBehavior *)behavior
      beganContactForItem:(id<UIDynamicItem>)item
   withBoundaryIdentifier:(id<NSCopying>)identifier
                  atPoint:(CGPoint)p {

    if (identifier == (id<NSCopying>)@"bottom") {
        [pushBlock removeItem:item];
        [blockCollision removeItem:item];
        [(UIView*)item removeFromSuperview];
    }
}

我从所有UIBehaviors和&#34; superview&#34;中删除项目。 现在我的对象是不可见的,但它仍然存在于内存中。 所以,我每秒创建两个新对象,但不能将其从内存中删除。

如何dealloc 阻止?如何从内存中删除? 或者我可能做错了,应该采用其他方式(例如,没有 performSelector: )?

P.S。 ARC已启用。

UPD:

问题出在 UILabel 中。如果删除所有 UILabel s ,则使用的内存不会增加并保持在同一级别。我忘了提及 UILabel 因为认为它并不重要。

有些人有类似的问题(this),但我仍然不明白发生了什么以及如何解决这个问题。

您可以在github上看到我的代码。

感谢。

2 个答案:

答案 0 :(得分:4)

如果您的对象没有强引用,则会自动取消分配。如果它位于视图层次结构中,则该层次结构会保留它,但您要将其从超级视图中删除。我相信UIDynamicBehavior子类会保留他们的项目,但您也会从那里删除您的项目。因此,只要您没有对该视图的任何其他强引用,它就会被取消分配。

要查看您是否遇到内存问题,请使用Allocations or Leaks instruments

答案 1 :(得分:0)

你永远不会自己解除对象的攻击。当对象的最后一个强引用消失时,对象会自动释放。除非您在某处拥有强引用,否则当从超级视图和包含它的列表容器中删除该对象时,该对象将被释放。