在当前的模态视图控制器中不调用dealloc方法

时间:2010-03-25 09:13:17

标签: iphone objective-c dealloc

它位于我的视图控制器

-(void)doctorsListAction
{
    if(isFirst == YES)
    {
      [self getDoctorsListController];
      [[self navigationController] presentModalViewController:doctorListViewNavigationController animated:YES];
      [doctorListViewController release];
    }       
}

-(void)getDoctorsListController
{
    //DoctorListViewController *doctorListViewController=[[[DoctorListViewController alloc]initWithNibName:nil bundle:nil]autorelease];

    doctorListViewController=[[DoctorListViewController alloc]init];
    doctorListViewNavigationController=[[UINavigationController alloc]initWithRootViewController:doctorListViewController];
    doctorListViewController.doctorList=doctorList;
    doctorListViewNavigationController.navigationBar.barStyle=  UIBarStyleBlackOpaque;
    [doctorListViewController release];
}

在DoctorListViewContrller

-(void)closeAction
{
    printf("\n hai i am in close action*******************************");
    //[doctorList release];
    //[myTableView release];
    //myTableView=nil;

    printf("\n myTableView retainCount :%d",[myTableView retainCount]);

    [[self navigationController] dismissModalViewControllerAnimated:YES];


}
//this method is not called I don't know why if it not called i will get memory issues

- (void)dealloc 
{
    printf("\n hai i am in dealloc of Doctor list view contrller");
    [doctorList release];
    [myTableView release];
    myTableView=nil;
    [super dealloc];
}

2 个答案:

答案 0 :(得分:1)

  

这个方法不叫我不知道   为什么如果不叫我会得到记忆   问题

当确切地调用dealloc时(即当对象被释放时)对你来说真的不重要。重要的是,您将每个allocrelease / autorelease配对。您可能没有这样做。

上面的代码看起来不太好,看起来有点“Java”-ish。你的“get”方法实际上并没有返回任何东西,这看起来很奇怪。但是你通常不会将方法命名为“get___”。

您可能在此行的getDoctorsListController方法中泄漏内存:

doctorListViewNavigationController=[[UINavigationController alloc]initWithRootViewController:doctorListViewController];

由于您没有在此方法中定义doctorListViewNavigationController,并且我假设您发布了编译的代码,因此它是您的类的成员(尽管不一定是属性)或某处的静态变量。这意味着它可能已经指向一个对象。这意味着当您为其分配新的alloc'ed对象时,旧的对象将丢失(泄露)。

以下是你应该如何重构它。

- (void)doctorsListAction
{
    if (isFirst == YES)
    {
        [self showDoctorsList];
    }       
}

- (void)showDoctorsList
{
      DoctorListViewController* doctorListViewController = [[DoctorListViewController alloc] initWithNibName:nil bundle:nil];
      doctorListViewController.doctorList = doctorList;
      UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:doctorListViewController];
      navController.navigationBar.barStyle = UIBarStyleBlackOpaque;
      [self.navigationController presentModalViewController:navController animated:YES];
      [navController release];
      [doctorListViewController release];
}

答案 1 :(得分:0)

可能有很多其他对象'幕后'想要保持DoctorListViewController。如果你只是平衡你的保留和发布,你应该没问题。

同样在-(void)doctorsListAction中,[doctorListViewController release];不应该[doctorListViewNavigationController release];代替?{/ p>