开发者

Strange Exc Bad Access when using Init, PushViewController, Release. Anything wrong with this code?

开发者 https://www.devze.com 2023-01-16 05:08 出处:网络
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 ro

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开发者_JAVA技巧 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
}


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!


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消