presentModalViewController and Retaincount

Discussion in 'iOS Programming' started by m4rc0s, Mar 26, 2010.

  1. m4rc0s macrumors newbie

    Joined:
    Apr 20, 2009
    #1
    Hi *,
    I just asked me how it behaves with the retaincount and presenting a view modal. First some code.

    I have the following action in my ViewController:
    Code:
    -(IBAction)showDetailView:(id)sender{
    	detailViewController = [[DetailViewController alloc] initWithDetails:(DetailObject *)detailObject];
    	[self presentModalViewController:detailViewController animated:NO];
    }
    In the DetailViewController there is another Action to close the DetailView:
    Code:
    -(IBAction)closeDetailView:(id)sender{
    	[self dismissModalViewControllerAnimated:YES];
    }
    So far, everything is working fine. I can load a detailed view for several objects and also leave the detailed view and switch back to my main view.

    But what about the retainCount of the(se) DetailViewController(s)? Everytime i call the action i allocate a detailview but by closing the detailview i do not release it.

    From the Logic i overwrite my variable "detailViewController" on every call. Do I also reserve memory for every call, or is it also overwritten?

    Is this a memory Leak? And if, how can I fix it.. :)

    Thanks a lot, Marco
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    It certainly looks like one. Fix it by releasing the object once you are done with it.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    Typically you release it like this:

    Code:
    -(IBAction)showDetailView:(id)sender{
    	detailViewController = [[DetailViewController alloc] initWithDetails:(DetailObject *)detailObject];
    	[self presentModalViewController:detailViewController animated:NO];
            [detailViewController release];
    }
     
  4. m4rc0s thread starter macrumors newbie

    Joined:
    Apr 20, 2009
    #4
    Thanks. I´ve tried that. But when I switch back from my detailed view the App ends with Bad Access then.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    I would suspect the issue lies elsewhere then. Better try some debugging to determine what specifically is causing the error. With that kind of info in hand, and relayed to us, we can better help you to troubleshoot.
     
  6. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #6
    Whenever you create a controller and present it (either using presentModalViewController, or by pushing on to a navigation stack), you can release it straight away as it will be retained by its new owner and released when removed (popped/dismissed).
     
  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    The code I showed is the correct way to manage this. If you're getting a crash when the modal is dismissed then it's probably due to an error in the view controller's dealloc method. You need to debug that.
     

Share This Page