Ios XCode:执行模式Segue时内存泄漏

Ios XCode:执行模式Segue时内存泄漏,ios,objective-c,memory-leaks,uistoryboardsegue,Ios,Objective C,Memory Leaks,Uistoryboardsegue,当登录用户打开我的应用程序时,它们将从我的AppDelegate发送到主TabBarController,如下所示: UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController; tabBar.selectedIndex = 2; // (this is MainViewController in the tab bar) [self performSegueWithIdentifier:

当登录用户打开我的应用程序时,它们将从我的AppDelegate发送到主TabBarController,如下所示:

UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
tabBar.selectedIndex = 2; 
// (this is MainViewController in the tab bar)
[self performSegueWithIdentifier:@"showChatSeg" sender:self];

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.destinationViewController isKindOfClass:ChatViewController.class]){
        ChatViewController *destinationViewController = (ChatViewController *)segue.destinationViewController;

        if(self.createdDialog != nil){
            destinationViewController.dialog = self.createdDialog;
            self.createdDialog = nil;
        }else{
            QBChatDialog *dialog = [ChatService shared].dialogs[self.selectedChat];
            destinationViewController.dialog = dialog;
        }
    }
}
- (IBAction)backButton:(id)sender {  
    [self performSegueWithIdentifier:@"fromChatToDashSeg" sender:nil];   

// This is a storyboard segue back to the MainTabBarController

}
现在,用户位于MainViewController中。当用户选择要进入的特定聊天时,会将其发送到ChatViewController(不在TabBarController上),如下所示:

UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
tabBar.selectedIndex = 2; 
// (this is MainViewController in the tab bar)
[self performSegueWithIdentifier:@"showChatSeg" sender:self];

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.destinationViewController isKindOfClass:ChatViewController.class]){
        ChatViewController *destinationViewController = (ChatViewController *)segue.destinationViewController;

        if(self.createdDialog != nil){
            destinationViewController.dialog = self.createdDialog;
            self.createdDialog = nil;
        }else{
            QBChatDialog *dialog = [ChatService shared].dialogs[self.selectedChat];
            destinationViewController.dialog = dialog;
        }
    }
}
- (IBAction)backButton:(id)sender {  
    [self performSegueWithIdentifier:@"fromChatToDashSeg" sender:nil];   

// This is a storyboard segue back to the MainTabBarController

}
当这种情况发生时,我会看到内存使用量激增,这是有道理的。但是,当用户离开ChatViewController并返回MainViewController时,如下所示:

UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
tabBar.selectedIndex = 2; 
// (this is MainViewController in the tab bar)
[self performSegueWithIdentifier:@"showChatSeg" sender:self];

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.destinationViewController isKindOfClass:ChatViewController.class]){
        ChatViewController *destinationViewController = (ChatViewController *)segue.destinationViewController;

        if(self.createdDialog != nil){
            destinationViewController.dialog = self.createdDialog;
            self.createdDialog = nil;
        }else{
            QBChatDialog *dialog = [ChatService shared].dialogs[self.selectedChat];
            destinationViewController.dialog = dialog;
        }
    }
}
- (IBAction)backButton:(id)sender {  
    [self performSegueWithIdentifier:@"fromChatToDashSeg" sender:nil];   

// This is a storyboard segue back to the MainTabBarController

}
我得到以下警告:

Attempt to present <MainTabBarController: 0x17ef28d0> on <ChatViewController: 0x17d6c940> whose view is not in the window hierarchy!
尝试显示不在窗口层次结构中的视图!

并且内存使用量保持不变。当用户再次进入聊天时,内存会继续增加。我没有正确地解除发送视图控制器吗?

这是因为您正试图执行一个序列以返回原始位置。您只需调用
[self dismissViewControllerAnimated:YES completion:nil]关闭当前的模态视图控制器。无论何时将模态视图添加到视图堆栈中,您都希望调用此方法退出,除非您的目的是在模态的顶部添加另一个视图

您所做的不是“返回”,而是在已有视图的基础上呈现上一视图的副本。这就是为什么记忆在积累,因为你只是不断地把越来越多的视图叠加在一起。假设您正在使用模式会话显示聊天视图,请尝试呼叫:

[self dismissViewControllerAnimated:YES completion:nil];

您是否正在使用模式会话来显示聊天?而不是使用segue返回。尝试调用[self-DismissViewControllerInitiated:YES completion:nil];使用此方法,我仍然会得到通知:尝试显示不在窗口层次结构中的视图!内存问题仍然存在。您在哪里定义了fromChatToDashSeg?这在你的故事板上吗?删除segue只是为了确保它仍然没有以某种方式使用segue,而不是正确地删除。谢谢,就是这样。另外,作为说明,我意识到我的tableView没有在ChatViewController中发布,所以我必须在-(void)dealloc{}中手动执行此操作