0

它在我的视图控制器中

-(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];
}

它在 DoctorListViewContrler

-(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];
}
4

2 回答 2

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];
}
于 2010-03-26T00:10:25.697 回答
0

可能有很多其他对象“在幕后”想要保留 DoctorListViewController。如果你只是平衡你的保留和释放,你应该没问题。

同样在-(void)doctorsListAction,不应该[doctorListViewController release];[doctorListViewNavigationController release];吗?

于 2010-03-25T14:29:17.970 回答