ARC Memory Not Released

Discussion in 'iOS Programming' started by SRegan, Jul 25, 2014.

  1. SRegan macrumors newbie

    Joined:
    Jul 25, 2014
    #1
    Hey Guys,

    I'm rather new to Objective C programming, but I do have several years of programming experience in various other languages.

    A few days ago I began working on an app (I'm using the storyboard if that matters) and when trying to "release" an object for memory cleanup, Xcode gave me an error saying I can't release an object with ARC. At that point I didn't know what ARC was, so I looked it up and read up on it and discovered it handles memory management for me. It sounded nice, so I decided to stick with it.

    Well, earlier today I was testing the app and noticed that when changing between view controllers, the memory from the previous controller is never released. I set break points on the "dealloc" methods and they aren't being triggered. I also used the static analyzer as well as the leak detector to see if I could track down the issue, however neither tool had any results.

    Now, I'm sure this has something to do with me horribly misunderstanding either ARC or some aspect of Objective C.

    On most properties (such as IBOutlets for UI elements) I use "weak" and I'm using "strong" only when necessary (like when an object needs to be allocated). Also, inside of any blocks I use a "weakSelf" ( __unsafe_unretained MyEventsController *weakSelf = self; )rather than "self".

    My project is a bit big, so I'd rather not post the entire thing. However, here are some snippets.


    One of my view controller's headers:
    Code:
    #import <UIKit/UIKit.h>
    
    @interface MyEventsController : UIViewController <UITableViewDelegate, UITableViewDataSource,UINavigationControllerDelegate>
    {
    
    }
    
    @property (nonatomic,weak) IBOutlet UITableView *myEventView;
    @property (nonatomic,strong) NSArray *myEventsArray;
    @property (nonatomic,strong) NSMutableArray *currentEvents;
    @property (nonatomic,strong) NSMutableArray *upcomingEvents;
    @property (nonatomic,strong) NSMutableArray *pastEvents;
    @property (nonatomic,strong) NSDateFormatter *dateFormat;
    @end
    

    Sometimes I use a navigationController to push to the next view controller. Here's the code I use from that, do I have to release(or "pop") the current controller if I'm using a navigationController?

    Code:
        EventController * controller = (EventController *)[self.storyboard instantiateViewControllerWithIdentifier:@"EventPage"];
        [self.navigationController pushViewController:controller animated:YES];
    

    Hopefully someone can help me find the issue.


    Thanks,
    Sean
     
  2. TheWatchfulOne macrumors 6502

    TheWatchfulOne

    Joined:
    Jun 19, 2009
    #2
    One possibility is you have created a retain cycle. This is what happens if, for example, object A owns object B and object B owns object A. Neither object will be released even with ARC. To fix this situation, make sure one side of the relationship between the two objects is weak. That is, if object A has a strong relationship to object B then object B should have a weak relationship to object A.
     
  3. SRegan, Jul 25, 2014
    Last edited: Jul 26, 2014

    SRegan thread starter macrumors newbie

    Joined:
    Jul 25, 2014
    #3
    Thanks for the reply. Is there any tool that can help me figure out which objects are being retained?

    Also, it might be worth mentioning that I'm using Parse SDK. Is it possible that an object from this is being retained?

    Edit: So I just went ahead and commented out most of the code in one of my view controllers.

    The only thing I kept was this in the header:

    Code:
    #import <UIKit/UIKit.h>
    @interface MyEventsController : UIViewController <UITableViewDelegate, UITableViewDataSource,UINavigationControllerDelegate>
    {
    
    }
    
    @property (nonatomic,weak) IBOutlet UITableView *myEventView;
    @end
    
    I stripped the implementation file down to the bare minimum, and yet dealloc was still never called and the memory was never released. Now I'm all sorts of confused.


    Edit 2: Found the issue! As I suspected, it was due to a huge misunderstanding on my part. I had thought that when pushing a view controller to the navigation controller it would automatically pop the view controller that you are navigating from. So when I was go from ViewController1 to ViewController2 I was pushing it to the navigation controller, but when I was going back from ViewController2 to ViewController1 I was still pushing to ViewController1 rather than poping ViewController2.

    This does bring up a question though. My initial view controller is a login screen, once the user is logged in, they will most likely hardly ever see the login screen (only if they log out) so I don't see a point in keeping the memory from it. When I push from my login screen to the main screen, is there anyway to pop or dealloc the login screen?

    Sean
     
  4. AxoNeuron, Jul 26, 2014
    Last edited: Jul 26, 2014

Share This Page