Memory management question

Discussion in 'iOS Programming' started by tiltem, Oct 20, 2010.

  1. tiltem macrumors newbie

    Joined:
    Oct 18, 2010
    #1
    I have navigation based template, using core data(not sure if that matters), and a UIViewController as a second view. I get to the second view by clicking the add (+) button in the top right of the navigation bar. In here I have the top right button of the navigation controller call my save method when clicked. In this save method I create a RootViewController object and call one of the rootView's methods and then pop back to the root view.

    The issue is that if I release this root view object the app crashes, if I do not release the object it seems to work fine. I just need to know if this is causing a memory leak?

    Here is the code for the save method, this is in my UIViewController, the RootViewController is a UITableViewController:

    Code:
    -(void) save{
    	if ([newEventName.text length]== 0) {
    		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Missing Event Name" message:@"Please enter an event name or Cancel." delegate:self cancelButtonTitle:@"DONE" otherButtonTitles:nil];
    		[alert show];
    		[alert release];
    		return;
    	}
    	eventDate = dateStartedPicked.date;
    	RootViewController *rvc =[[RootViewController alloc] initWithNibName:nil bundle:nil];
    	[rvc addEventName:newEventName.text eventDate:eventDate];
    	[self.navigationController popToRootViewControllerAnimated:YES];
    //	[rvc release];   <--- Dies here if released
    }
    
    I feel like the rvc object is now considered self since I have popped back to its view, and thus may not need to be released. I don't know but would like to know for sure.

    Thanks!!!
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Why create another RootViewController when you already have one instantiated?: the one controlling the initial table view and that calls up the second view. Rather than create a new one, find a way to get access to the existing one.

    No, the rvc object is not self; self will always be the current instance of the object of the class that you're within.
     
  3. tiltem thread starter macrumors newbie

    Joined:
    Oct 18, 2010
    #3
    Yes I would rather not create another RootViewController, but am unsure about how to reference the existing one. How would I reference the original RootViewController object from within the new UIViewController?
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    One way would be to tell it about it (i.e. set up a property for the RootViewController in the UIViewController and then set it after you've instantiated the UIViewController.)
     

Share This Page