0

I have UICollisionBehavior and some boundaries (addBoundaryWithIdentifier:fromPoint:toPoint:): on left, right and bottom side of screen.

I do this (just falling blocks, like a rain):

- (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];
}

Then, when collision is happened:

- (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];
    }
}

I remove item from all UIBehaviors and from "superview". Now my object is invisible, but it still exist in memory. So, I create two new object every second, but not remove it from memory.

How to dealloc block? How to remove block from memory? Or maybe i do all wrong and should use another way (e.g. without performSelector:)?

P.S. ARC is enabled.

UPD:

The problem is in UILabel. If delete all UILabels, then memory utilized is not increase and stay on same level. I forgot mention UILabel because thought that it doesn't matter.

Some people have similar problem (this), but i still do not understand what's going on and how to fix that.

You can see my code on github.

Thanks.

Community
  • 1
  • 1
evlogii
  • 811
  • 1
  • 7
  • 17
  • Does it hurt if it remains in memory (i.e. are you actually running into memory problems)? – Sebastian Wramba May 19 '14 at 19:09
  • @SebastianWramba i think memory leaks is bad and hurts every time. – evlogii May 19 '14 at 19:31
  • True, just thought there might not be a memory problem because the objects will eventually get wiped, but if it's leaking ... – Sebastian Wramba May 19 '14 at 19:33
  • To force objects with a zero retain count from memory, you can bracket the deletion code in `@autoreleasepool{}` -- that will call dealloc on released objects sooner rather than later. – stevesliva May 19 '14 at 20:29
  • @stevesliva this description is not exactly correct. A release pool is a way to call `-release` on autoreleased objects at a time of your choosing, rather than at the end of the current tick of the run loop, which is when the default pool drains. – Zev Eisenberg May 19 '14 at 20:52
  • You should be using `-isEqualToString:` instead of `==` with a cast to `id` for string comparison. – Zev Eisenberg May 19 '14 at 20:57
  • @ZevEisenberg -- unsure what's incorrect based on your comment. I qualified with "zero retain count." Are you saying the time of deallocation isn't specific to when the pool drains? – stevesliva May 19 '14 at 20:57
  • @stevesliva if you don’t put the objects in a local autorelease pool, they will not have the final `-release` called on them until the global pool drains on the next tick of the run loop. It is therefore inaccurate to refer to it as an object with a zero retain count. Also, `-retainCount` can by definition never return `0`. – Zev Eisenberg May 19 '14 at 21:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/53992/discussion-between-zev-eisenberg-and-stevesliva). – Zev Eisenberg May 19 '14 at 21:04

2 Answers2

4

If your object has no strong references to it, it will be deallocated automatically. If it is in a view hierarchy, that hierarchy retains it, but you are removing it from its superview. I believe UIDynamicBehavior subclasses retain their items, but you are removing your item from there as well. So as long as you don‘t have any other strong references to the view, it will be deallocated.

To see if you are having memory issues, use the Allocations or Leaks instruments.

Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82
0

You never dealloc an object yourself. An object is automatically deallocated when the last strong reference to the object goes away. Unless you hold a strong reference somewhere, the object will be deallocated when it is removed from its superview and the lists containers containing it.

gnasher729
  • 51,477
  • 5
  • 75
  • 98