简体   繁体   中英

Properly passing NSManagedObjects between views when using ARC? Memory warning & crash

Hi I have a nasty memory management issue under ARC and cannot figure out how to solve it. The problem stands like this, I have these objects:

ObjectManager - a singleton that does fetchRequests to core data and fetches one or more NSManagedObjects

UIViewControllerA - a view controller in which I have a button "PassManagedObject" and a property declared as below:

@property (strong, nonatomic) ManagedObject *objectForToday;

in viewDidLoad on UIViewControllerA , I call the method refreshDailyObject which does this:

self.objectForToday = nil;
self.objectForToday  = [[ObjectManager sharedManager] getDailyObject];

if I tap the PassManagedObject button I create UIViewControllerB , pass the objectForToday to it and display it, see below

- (IBAction)passManagedObjectTapped:(id)sender {
       UIViewControllerB *viewController = [[UIViewControllerB alloc] initWithNibName:@"UIViewControllerB"];
       viewController.object = self.objectForToday;
       [self.navigationController pushViewController:viewController animated:NO];
 }

UIViewControllerB has a property declared like this:

@property (strong, nonatomic) ManagedObject *object;

and a button " Back " which does this:

- (IBAction)backAction:(id)sender {
    self.object = nil;
    [self.navigationController popViewControllerAnimated:NO];
}

Now the problem is this. If I tap continuously passManagedObjectTapped and then backAction, and again passManagedObjectTapped and again backAction and then again passManagedObjectTapped and automate this I'm getting eventually Received Memory Warning 1 , and then crash .

The Instruments doesn't show any leaks but my memory allocation keeps slowly going up.

I am using ARC under iOS4.3 & iOS5. I've been struggling to figuring out what is wrong for a day now. Any help will be highly appreciated.

Thanks!

The self.object = nil; and self.objectForToday = nil; isn't necessary - ARC and the synthesized properties setters already take care of that.

It seems pretty likely that you have a circular reference somewhere. Do you have any situations where object A has a strong reference to object B and object B has a strong reference to object A?

If so, just change one of those references to weak instead (or assign if you want to support iOS 4.3).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM