繁体   English   中英

当前模态视图控制器中未调用dealloc方法

[英]The dealloc method is not called in the present modal view contrller

在我的视图控制器中

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

不调用此方法,我不知道为什么不调用该方法会出现内存问题

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

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

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

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

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

这是重构的方式。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM