Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1116289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:16:45+00:00 2026-05-17T03:16:45+00:00

I put an NSlog in my dealloc method of my view controller. It does

  • 0

I put an NSlog in my dealloc method of my view controller. It does not get consistently called. I do notice that ViewWillDisappear does get called always. Would it be ok to move all my tidy upcode here? Setting stuff to Nil and release calls.

Anybody got some advice on why dealloc is not getting called? I know it says in docs it may not get called, but if you have a very simple App it gets called always. So something i do must be affecting the dealloc.

This is the code that calls my ViewController than isnt always calling my dealloc.

-(IBAction) playComputerTapped:(id)sender
{

PlayGameViewController *pgvc = [[PlayGameViewController alloc]
initWithNibName:@”PlayGameViewController” bundle:[NSBundle mainBundle]];
pgvc.gameMode = 1;
[self presentModalViewController:pgvc animated:YES];
[pgvc release];
}

The above code takes me from the mailmenu ViewController into the game.

Below is the code to leave the gameViewController and take me back to the menu.

[self.parentViewController dismissModalViewControllerAnimated:YES];

Thanks
-Code

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T03:16:45+00:00Added an answer on May 17, 2026 at 3:16 am

    Don’t you mean viewDidUnload instead of viewWillDisappear ?

    viewWillDisappear is called when the view controller is disappearing. This usually happens when the view controller is being popped out, or other view controller is pushed to the stack. Purpose of viewWillDisappear is to stop active actions – for example stop animations, hide some elements or similar.

    viewDidUnload is probably what you meant as this one is called when view controller’s view is unloaded. This should never happen for a view controller that is currently visible, only for controllers that are somewhere in the navigation stack (part of UITabBarController or UINavigationController), but not currently visible. Purpose of viewDidUnload is to release any UI elements that are part of the view and that the view controller retained as well.

    To understand it’s important to realize the reasons why a view for such controller would want to be unloaded. Reason is memory consumption. Views consume considerable amount of memory, even when they are not currently visible. Views are usually very easy to reconstruct – simply by calling the code that constructed them in the first place. That’s especially very easy if you are using Interface Builder. So these views are best candidates to be freed to gain more memory.

    When system doesn’t have enough memory it starts calling didReceiveMemoryWarning method of view controllers which are not currently visible. If you have created your controllers from template in Xcode, this method simply calls super’s (UIViewControllers’s) implementation [super didReceiveMemoryWarning]. That default implementation will release self.view, which in turn should deallocate it together with all its subviews.

    Now let’s say that your view-controller needs access to some of the subviews to manipulate it in some way. For example you can have a UILabel element there and you want to change its content accordingly. To access this element you create a member variable (IBOutlet) and connect it to the element. Now your view-controller owns that label, so its retain-count is increased. When controller’s view is released, so is the label, but because your view-controller still retains the label, it will not be deallocated. Therefore you should release the label in viewDidUnload method.

    I’ve seen applications that were creating views programmatically (in loadView method), but loading was done in such dirty way that it was not possible to reconstruct the view after it was once deallocated. Therefore each time system was out of memory, it had called didReceiveMemoryWarning which in turn deallocated the view, but after navigating back to that view-controller application had crashed. A fast “bugfix” was to remove calling [super didReceiveMemoryWarning] in view-controllers. Well, system didn’t get the memory and some strange effects occurred, but at least the application didn’t crash immediately.

    Now the third one – dealloc. This is called when object is not owned by anyone and its memory is going to be freed. Here you need to release all objects that you have retained. For view-controllers those are usually references to model classes.

    I want to describe one more possible scenario. Let’s say you have a view-controller displaying a chat with another person. Let’s say it’s very fancy chat, with emoticons and buddy-icons being animated. Let’s say that each chat-entry is displayed as a cell of UITableView.

    When your buddy sends you a message, you want to append a new cell into table-view by reloading it. Therefore your view-controller has an outlet to the table-view.

    In viewWillDisappear you should stop the animations of emoticons and icons.
    In viewDidUnload you should release the table-view.
    In dealloc you want to release chat’s history (probably NSArray of all messages sent and received during this conversation).

    Now if you navigate away from your chat, viewWillDisappear gets called and you stop the animations.

    When system is low on memory, and your view-controller is not visible, didReceiveMemoryWarning gets called and the view is released. Your viewDidUnload gets called and you release UITableView so that it can be really deallocated.

    When you navigate back to the chat, loadView gets called again and your view is constructed again, viewDidLoad is called after that. Your model (representation of chat conversation) is still there, so the table-view’s datasource has all the data as before, so table-view will display exactly the same thing as before the view was deallocated. After all viewWill/DidAppear is called where you start the animations.

    When you finish chatting with your friend, you release the view controller, and it gets deallocated – dealloc is called, you release an array containing the chat messages, and cleanup everything else.

    I hope it make things a little clearer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NSLog puts something into stderr, is there a Objective-c method that can put NSString
I put an NSLog statement inside my applicationDidFinishLaunching method. I build and run and
I did an experiment in viewDidLoad where I put: NSLog(@The View did load :-)
I have a brain class that I want to get Rest services and put
When I use XCode I (obviously) put a bunch of NSLog statements in to
I put together this quick carousel that I'm using to cycle through different divs
I put my files in the directory assets. how do I get them? I
Possible Duplicate: Why is object not dealloc'ed when using ARC + NSZombieEnabled I've got
I put NSLog(@%@::%@, [[self class] description], NSStringFromSelector(_cmd)); in both viewDidLoad and viewDidUnload of a
I have method that download and resize image according to screen size to fit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.