iOS - viewController dealloc method not called after popping to previous viewController

StackOverflow https://stackoverflow.com/questions/23388962

  •  12-07-2023
  •  | 
  •  

Domanda

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];
}
È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top