iOS - viewController dealloc method not called after popping to previous viewController
Asked Answered
C

2

5

In iOS, I pop from current viewController into previous one, but it doesn't go into dealloc.

Is this because there is another pointer pointing towards the current viewController, either in a different viewController or in the current one?

This is where I pop to previous view:

- (IBAction)fileUploadCancelTouched:(UIButton *)sender {

    [self.fileToUpload cancel];

    [self.view hideToastActivity];
    [self.greenprogressBar removeFromSuperview];
    [self.subView removeFromSuperview];
    self.fileUploadCancelButton.hidden = YES;
    if (self.commandComeBackToFinalScreen == 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

This is my dealloc function:

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    self.greenprogressBar = nil;
    self.fileUploadCancelButton = nil;
    self.fileToUpload = nil;
    [buttonHome_ release];
    [buttonTestMeAgain_ release];
    [buttonMarkMyTest_ release];
    [examId_ release];
    [sender_ release];
    self.ob = nil;
    [_fileUploadCancelButton release];
    [super dealloc];
}
Cosimo answered 30/4, 2014 at 13:7 Comment(1)
Please provide some code for better understanding your question.Tomasatomasina
S
6

Check to make sure that ARC is not enabled in your project. If it is not ARC enabled then dealloc should be called unless your code is retaining your view controller. You should check through the Instruments tool if your pop commands reduces memory or not.

There may be some other reasons as mentioned in another answer that I am posting below: The obvious reason is that something is retaining your viewController. You will have to look closely at your code. Do you do anything that in your class that uses delegates, since they sometimes retain the delegate. NSURLConnection will retain your class, and so does NSTimer. You can scatter code in you class and log your class's retain count, and try to find out where. In the code you showed so far the retain could should just be 1, since the class is only retained by the navigation controller.

Also, before you pop your view, get a reference to it, pop it with NO animation, and then send it some message that has it report the retain count (this would be some new method you write). That new method could also log other things, like whether it has any timers going, NSURLConnections, etc.

Sawdust answered 30/4, 2014 at 13:15 Comment(5)
[NSTimer scheduledTimerWithTimeInterval:12 target:self selector:@selector(sendTest) userInfo:nil repeats:NO]; would this retain the viewController? and if so, would the invalidate function fix this?Cosimo
@Khawar Ali please do not recommend that people use -retainCount: whentouseretaincount.com.Courson
@Cosimo yes, invalidating the timer will release its target. From the documentation for that timer method: The timer maintains a strong reference to target until it (the timer) is invalidated..Courson
NSTimer was the culpritCosimo
Thank you, needed to reed that the NSURLConnection does retainInexhaustible
M
1

First of all, get rid of [super dealloc]. I know that's intuitive, but the documentation says don't do it.

In my own case, I had an observer & timer in my dealloc method, but that wouldn't run since the timer had a strong pointer to the controller.

Created a dedicated clean up method which removed the observer & invalidated the timer. Once that ran, the controller was correctly deallocated.

Meerschaum answered 28/10, 2015 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.