1

Questions about the general model of how I should be programming in iOS.

I started before ARC, doing manual memory management. I was originally taught to make every class variable a property and release in dealloc. This model works great, when I push and pop navigation controllers LIVE BYTES alloc and dealloc respectively.

When I switched to ARC however this is not the case. My live bytes never seem to go down, even when popping a navigation controller. I don't get it, am I not supposed to use properties? I generally use a strong property for anything except for an IBOutlet in which I case I'll use a weak property.

Is there something I'm missing? Do I need to do something in viewDidUnload or implement my own dealloc method???

If I use my app for long enough, I'll eventually receive a memory warning and crash. So I know something's not right.

MobileMon
  • 8,341
  • 5
  • 56
  • 75
  • for example if I have an array of data in my class here's how I do it: @property(strong) NSMutableArray *arrayOfData. I'll initialize and add to it but thats it. I never set it to nil or anything like that when the view disappears – MobileMon May 07 '13 at 14:12
  • Setting it to nil as you suggested will definitely kill it quickly. Is there a reason you don't wish to do that? If you are experiencing memory warnings I highly doubt it is from that array though. – Mark McCorkle May 07 '13 at 14:23
  • @MarkM No there's not a reason I don't wish to do that. I just thought with ARC I didn't have to. Would you suggest setting all properties to nil then in viewDidUnload (or maybe viewDidDisappear)? – MobileMon May 07 '13 at 14:32
  • Honestly, I never do and I haven't experienced issues like you are describing. Have you fired up instruments to see what is leaking out? Also, try analyze from the build menu. – Mark McCorkle May 07 '13 at 14:33
  • @MarkM yes I use instruments, that's how I see the live bytes never release from pushing and popping. No leaks ever occur, just a continual, gradual incline of live bytes – MobileMon May 07 '13 at 14:34
  • You probably have strong references to these objects elsewhere. – omz May 07 '13 at 14:57
  • 1
    viewDidUnload is no longer called (in iOS 6). What you're describing shouldn't happen. If you pop a view controller off the stack it will be deallocated (unless you're keeping a pointer to it). With ARC, there's no need to set properties to nil in dealloc. – rdelmar May 07 '13 at 14:57
  • 1
    Perhaps you have a retain cycle? This could happen for example, if two view controllers retain each other (both having a strong property that contains the other view controller). This is a situation ARC cannot deal with automatically, so you have to make one of the properties weak. (A delegate is an example of a property you almost always need to make weak in order to avoid leaks) – omz May 07 '13 at 14:59
  • To add to what omz is saying (good advice), I always set delegates to (nonatomic, assign) to avoid this. FYI – Mark McCorkle May 07 '13 at 15:07
  • Ok perhaps I have a retain cycle. Lets say I have 2 viewControllers that I push to (A and B), a custom class called "Person", and I have a property (strong) Person *person on class A. Now I want to pass this person to class B who also has a property (strong) Person *person. So if I set class A's person equal to class B's person in the prepareForSegue method, will this cause a retain cycle? (The person class does not contain any delegates, only attributes like name, address, etc which are of type NSString)? – MobileMon May 07 '13 at 15:22
  • @MarkM I was retaining a delegate so YES THAT DEFINITELY HELPED. However I still have an issue with the live bytes increasing (although much smaller now). I'd still like to know the answer to my above question – MobileMon May 07 '13 at 15:23
  • 1. You describe monitoring of live bytes. Have you run the [leaks instrument](https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/MemoryManagementforYouriOSApp/MemoryManagementforYouriOSApp.html#//apple_ref/doc/uid/TP40004652-CH11-SW4)? 2. Have you run the code through the static analyzer ("Analyze" on "Product" menu)? 3. If you turned on zombies, have you turned that off? 4. Have you [looked at the allocations](http://stackoverflow.com/questions/14104556/ios-app-with-arc-find-who-is-owner-of-an-object/14105056#14105056) to identify them? – Rob May 08 '13 at 16:35

0 Answers0