views:

99

answers:

3

Maybe I've been looking at this for too long ;) My app has a NavigationController and several ViewControllers. From one of the ViewControllers two levels down (mainViewController), loaded from the rootViewController, I have the code below. After the PushViewController to the dataViewController and back (e.g. back Button pressed), the app crashes.

The dataViewController loads just fine, but when the back button of the navigationController is tapped, the App crashes with Object Exception. If I remove:

[dataViewController release];

the app works fine. It's strange because the dataViewController is init'ed in the same method. Any ideas?

- (void) locationPage 
{

    [[NSNotificationCenter defaultCenter] postNotificationName:@"NotifyRemoveMap" object:nil];
    MyAppDelegate *app = [[UIApplication sharedApplication] delegate];

    UINavigationController *navigation = app.navigationCantroller;
    [navigation popToRootViewControllerAnimated:NO];

    DataViewController *dataViewController = [[DataViewController alloc] initWithNibName:@"DataView" bundle:nil];
    [dataViewController setCategoryId:category];

    MyLanguage *lang = app.lang;
    Mylocation *location = [lang locationForCategoryId:category];

    dataViewController.title = location.name;
    NSArray *locationArray = [lang locations];

    dataViewController.locations = locationArray;
    [navigation pushViewController:dataViewController animated:YES];
    [dataViewController release]; //  With this removed, app doesn't crash
}
+1  A: 

The problem probably arises when the dataViewController gets popped and you try to access something on it - it is already released then. You might check the console for more details - better yet, run in debug mode (debug configuration and running with debugger).

You can edit your question to show some code that is run with the back button.

Eiko
Nothing logs to console. Just crashes hard. Xcode 3.2.3, 3.2.4, iOS 3.1. Stepping through debugger doesn't reveal anything. Interesting comment, but my understanding is that the pushViewController retains dataViewController until the back button is hit.
Jordan
It does retain, but the second it gets popped it's released. So when you want to read some data from it you better had retained it before.
Eiko
A: 

You talk about releasing dataViewController but your code says detailsViewController. Did you copy and paste incorrectly or is that the mistake?

You should consider not to use app.navigationController but self.navigationController. Cleaner design. Less dependencies on the app delegate, which too often is used as a frankensteinobject that knows too much.

St3fan
edited: should be dataViewController. thanks. Still a problem.
Jordan
When I use self.navigationController, the PushViewController doesn't get loaded. Just goes to the root - [navigation popToRootViewControllerAnimated:NO];
Jordan
A: 

Haven't even read your post. If it's Exec-Bad-Access, I have 2 words for you:

Enable NSZombies.

Follow this link: (it explains everything you need to know to fix any bad access issue)

Phone Memory Debug with NSZombie and Instruments

Cheers!

lupu1001
BINGO! Lupu1001, your answer saved my life! ;) I LOVE YOU MAN!!!
Jordan
Glad I could help. See you around.
lupu1001