-1

ARC view in instrument

Hi All

I am trying to dealloc a ViewController in ARC mode. However, the RefCount is always non-zero.

I have tried to set all object to nil and all subviews to removeFromSuperview + nil; and timer to invalidate + nil; still the counter = 2;

Is there a way to trace which pointer is still in retain?

Thanks

MobileDev
  • 178
  • 2
  • 13

2 Answers2

2

If you are using blocks you might also create retain cycle there. E.g. a block is referenced by an object and inside this block you are referencing object or calling instance method for object. Another option for retain count not dropping to 0 is that you have registered abject as observer for notification.

You might find this answer helpful: https://stackoverflow.com/a/12286739/2261423

Example of strong reference cycle from apple docs:

self.block = ^{
        [self doSomething];    // capturing a strong reference to self
                               // creates a strong reference cycle
    };
Community
  • 1
  • 1
tsr
  • 324
  • 2
  • 8
  • Yes this is likely the issue, I have made a lot of block in between... is there any fast search to find out which block is retaining the controller? – MobileDev Aug 29 '14 at 14:05
  • It's the block you keep a reference to in you object. It's nicely shown in apple docs: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html section "Avoid Strong Reference Cycles when Capturing self" self.block = ^{ [self doSomething]; // capturing a strong reference to self // creates a strong reference cycle }; – tsr Aug 29 '14 at 14:20
  • Compiler would throw a warning, if there was a retain cycle with block. – kelin Aug 29 '14 at 15:10
  • Compiler would throw a warning if it can recognise the situation, but it can't always do that. And of course you need to turn warnings on!!! – gnasher729 Mar 23 '15 at 19:26
-1

@Billy, why are you doing this? You may not worry about deallocation when using ARC. Controller will be deallocated automatically, when there will be no references to the controller. Yes, views do not refer to controller, they are referred by it! So removing that views will not affect retain count of the controller. If you really want to remove View Controller from memory, remove it from parent view controller and set all links to nill.

kelin
  • 9,553
  • 6
  • 63
  • 92
  • thanks for advice. Because the memory is kept high and I want to free the memory up, thus I try to put a breakpoint in dealloc and it is somehow not called. It means I have some "hidden" pointer referring to the controller. It is very difficult to find it all out... – MobileDev Aug 29 '14 at 14:00