Question

I've spent a great deal troubleshooting why the my view controller isn't being released for memory when It get's popped off the stack. Here is the cliffnotes version of what I am working with:

- (IBAction)submitTurn:(UIButton *)sender {
    ...
    [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error){
        if (!error){
            ...
            [game saveEventually];
            ...
            [push sendPushInBackground];
            }
        }
        else{
            NSLog(@"Error submitting turn: %@ %@", error, [error userInfo]);
            [[[UIAlertView alloc] initWithTitle:@"Error Saving Game" message:@"Try again later." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
        }
    } progressBlock:nil];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

When I run my app and keep coming back to this view controller and submitting my turn, instruments/leaks shows me that I have multiple live instances of this view controller. However, when I comment out all the image/game/push saving and just pop the view, then the memory gets released and I never have more than one live instance.

I also tried this to ensure that everything was saved and finished before poppingToRoot.

- (IBAction)submitTurn:(UIButton *)sender {
    ...
    [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error){
        if (!error){
            ...
            [game saveEventually:^(BOOL succeeded, NSError *error) {
                ...
                    [push sendPushInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                        [self.navigationController popToRootViewControllerAnimated:YES];
                    }];
                }
            }];
        }
        else{
           ...
        }
    } progressBlock:nil];
}

Still, no dice. How do you pop back to the root without it leaving artifacts?

Was it helpful?

Solution

You probably have a retain cycle created by the block when accessing self.

Try this:

__unsafe_unretained YourClass* self_ptr = self;

before entering the block, and within the block use self_ptr instead of self.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top