2

我试图在我的代码中找到内存泄漏,但不明白问题出在哪里。在我的 iOS 应用程序中,我使用滑动菜单导航模式。导航控制器可以使用滑动菜单进行切换。注销后,应释放所有控制器并释放内存。但是,尽管调用了 dealloc 并且 Instruments 中的引用计数似乎正常,但一些控制器在注销后仍留在内存中。

设置视图控制器

#   Category    Event Type  RefCt   Responsible Library Responsible Caller
0   SettingsViewController  Malloc  1   MyApp   -[SlideViewController didSelectMenuItem:]
1   SettingsViewController  Autorelease     MyApp   -[SlideViewController didSelectMenuItem:]
2   SettingsViewController  Retain  2   UIKit   -[UIViewController _addChildViewController:performHierarchyCheck:]
3   SettingsViewController  Retain  3   UIKit   -[UINavigationController topViewController]
4   SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
5   SettingsViewController  Retain  4   UIKit   -[UINavigationController topViewController]
6   SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
7   SettingsViewController  Release 3   Foundation  __NSFireDelayedPerform
8   SettingsViewController  Release 2   Foundation  __NSFireDelayedPerform
9   SettingsViewController  Release 1   Foundation  __NSFireDelayedPerform
10  SettingsViewController  Retain  2   UIKit   -[UINavigationController topViewController]
11  SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
12  SettingsViewController  Retain  3   UIKit   -[UINavigationController topViewController]
13  SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
14  SettingsViewController  Retain  4   UIKit   -[UINavigationController topViewController]
15  SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
16  SettingsViewController  Retain  5   UIKit   -[UINavigationController topViewController]
17  SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
18  SettingsViewController  Retain  6   UIKit   -[UINavigationController topViewController]
19  SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
20  SettingsViewController  Release 5   GraphicsServices    GSEventRunModal
21  SettingsViewController  Release 4   GraphicsServices    GSEventRunModal
22  SettingsViewController  Release 3   GraphicsServices    GSEventRunModal
23  SettingsViewController  Release 2   GraphicsServices    GSEventRunModal
24  SettingsViewController  Release 1   GraphicsServices    GSEventRunModal
25  SettingsViewController  Retain  2   UIKit   -[UINavigationController topViewController]
26  SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
27  SettingsViewController  Retain  3   UIKit   -[UINib instantiateWithOwner:options:]
28  SettingsViewController  Retain  4   UIKit   +[UIProxyObject addMappingFromIdentifier:toObject:forCoder:]
29  SettingsViewController  Retain  5   UIKit   -[UIProxyObject initWithCoder:]
30  SettingsViewController  Retain  6   UIKit   -[UIRuntimeConnection initWithCoder:]
31  SettingsViewController  Retain  7   UIKit   -[UIRuntimeConnection initWithCoder:]
32  SettingsViewController  Retain  8   UIKit   -[UIRuntimeConnection initWithCoder:]
33  SettingsViewController  Retain  9   UIKit   -[UIRuntimeConnection initWithCoder:]
34  SettingsViewController  Retain  10  UIKit   -[UIRuntimeConnection initWithCoder:]
35  SettingsViewController  Retain  11  UIKit   -[UIRuntimeConnection initWithCoder:]
36  SettingsViewController  Retain  12  UIKit   -[UIRuntimeConnection initWithCoder:]
37  SettingsViewController  Retain  13  UIKit   -[UIRuntimeConnection initWithCoder:]
38  SettingsViewController  Retain  14  UIKit   -[UIRuntimeConnection initWithCoder:]
39  SettingsViewController  Retain  15  UIKit   -[UIRuntimeConnection initWithCoder:]
40  SettingsViewController  Retain  16  UIKit   -[UIRuntimeConnection initWithCoder:]
41  SettingsViewController  Retain  17  UIKit   -[UIRuntimeConnection initWithCoder:]
42  SettingsViewController  Retain  18  UIKit   -[UIRuntimeConnection initWithCoder:]
43  SettingsViewController  Retain  19  UIKit   -[UIRuntimeConnection initWithCoder:]
44  SettingsViewController  Retain  20  UIKit   -[UIRuntimeConnection initWithCoder:]
45  SettingsViewController  Retain  21  UIKit   UINibDecoderDecodeObjectForValue
46  SettingsViewController  Retain  22  UIKit   UINibDecoderDecodeObjectForValue
47  SettingsViewController  Release 21  UIKit   -[UINib instantiateWithOwner:options:]
48  SettingsViewController  Release 20  UIKit   +[UIProxyObject removeMappingsForCoder:]
49  SettingsViewController  Release 19  UIKit   -[UIRuntimeConnection dealloc]
50  SettingsViewController  Release 18  UIKit   -[UIRuntimeConnection dealloc]
51  SettingsViewController  Release 17  UIKit   -[UIRuntimeConnection dealloc]
52  SettingsViewController  Release 16  UIKit   -[UIRuntimeConnection dealloc]
53  SettingsViewController  Release 15  UIKit   -[UIRuntimeConnection dealloc]
54  SettingsViewController  Release 14  UIKit   -[UIRuntimeConnection dealloc]
55  SettingsViewController  Release 13  UIKit   -[UIRuntimeConnection dealloc]
56  SettingsViewController  Release 12  UIKit   -[UIRuntimeConnection dealloc]
57  SettingsViewController  Release 11  UIKit   -[UIRuntimeConnection dealloc]
58  SettingsViewController  Release 10  UIKit   -[UINibDecoder finishDecoding]
59  SettingsViewController  Release 9   UIKit   -[UIRuntimeConnection dealloc]
60  SettingsViewController  Release 8   UIKit   -[UINibDecoder finishDecoding]
61  SettingsViewController  Release 7   UIKit   -[UIRuntimeConnection dealloc]
62  SettingsViewController  Release 6   UIKit   -[UIRuntimeConnection dealloc]
63  SettingsViewController  Release 5   UIKit   -[UINibDecoder finishDecoding]
64  SettingsViewController  Release 4   UIKit   -[UIRuntimeConnection dealloc]
65  SettingsViewController  Release 3   UIKit   -[UIRuntimeConnection dealloc]
66  SettingsViewController  Release 2   UIKit   -[UIRuntimeConnection dealloc]
67  SettingsViewController  Retain  3   UIKit   -[UINavigationController topViewController]
68  SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
69  SettingsViewController  Retain  4   UIKit   -[UINavigationController topViewController]
70  SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
71  SettingsViewController  Retain  5   UIKit   -[UINavigationController _startTransition:fromViewController:toViewController:]
72  SettingsViewController  Release 4   UIKit   -[UINavigationController _startTransition:fromViewController:toViewController:]
73  SettingsViewController  Retain  5   UIKit   -[UINavigationController topViewController]
74  SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
75  SettingsViewController  Retain  6   UIKit   -[UIResponder becomeFirstResponder]
76  SettingsViewController  Release 5   UIKit   -[UIResponder becomeFirstResponder]
77  SettingsViewController  Retain  6   UIKit   -[UINavigationController topViewController]
78  SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
79  SettingsViewController  Retain  7   UIKit   -[UINavigationController topViewController]
80  SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
81  SettingsViewController  Retain  8   UIKit   -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:]
82  SettingsViewController  Release 7   UIKit   -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:]
83  SettingsViewController  Retain  8   UIKit   -[UINavigationController topViewController]
84  SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
85  SettingsViewController  Retain  9   UIKit   -[UINavigationController topViewController]
86  SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
87  SettingsViewController  Release 8   UIKit   _wrapRunLoopWithAutoreleasePoolHandler
88  SettingsViewController  Release 7   UIKit   _wrapRunLoopWithAutoreleasePoolHandler
89  SettingsViewController  Release 6   UIKit   _wrapRunLoopWithAutoreleasePoolHandler
90  SettingsViewController  Release 5   UIKit   _wrapRunLoopWithAutoreleasePoolHandler
91  SettingsViewController  Release 4   UIKit   _wrapRunLoopWithAutoreleasePoolHandler
92  SettingsViewController  Release 3   UIKit   _wrapRunLoopWithAutoreleasePoolHandler
93  SettingsViewController  Release 2   UIKit   _wrapRunLoopWithAutoreleasePoolHandler
94  SettingsViewController  Release 1   UIKit   _wrapRunLoopWithAutoreleasePoolHandler
//controller is loaded now 
//then logout button will be pressed and the memory should be released
95  SettingsViewController  Retain  2   UIKit   -[UINavigationController topViewController]
96  SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
97  SettingsViewController  Release 1   UIKit   -[UIViewController _setViewAppearState:isAnimating:]
98  SettingsViewController  Retain  2   UIKit   -[UINavigationController topViewController]
99  SettingsViewController  Autorelease     UIKit   -[UINavigationController topViewController]
100 SettingsViewController  Release 1   UIKit   -[UIViewController _setViewAppearState:isAnimating:]

此日志显示最后的引用计数为 1,并且对象没有过度保留。另一个控制器被正确释放。它的仪器日志看起来相同,但在日志末尾还有 2 个额外的调用:

//controller is loaded now 
//then logout button will be pressed and the memory should be released    
75  SummaryViewController   Retain  2   UIKit   -[UINavigationController topViewController]
76  SummaryViewController   Autorelease     UIKit   -[UINavigationController topViewController]
77  SummaryViewController   Release 1   UIKit   -[UIViewController _setViewAppearState:isAnimating:]
78  SummaryViewController   Retain  2   UIKit   -[UINavigationController topViewController]
79  SummaryViewController   Autorelease     UIKit   -[UINavigationController topViewController]
80  SummaryViewController   Release 1   UIKit   -[UIViewController _setViewAppearState:isAnimating:]
81  SummaryViewController   Release 0   UIKit   -[UIViewController dealloc]
82  SummaryViewController   Free    0   UIKit   -[UIViewController dealloc]

调试显示为两个控制器调用了 dealloc。而且我不明白为什么第一个控制器保留在内存中,但第二个控制器被正确释放。

SettingsViewController 的分配:

self.settingsNavigationController = [[[UINavigationController alloc] initWithRootViewController:[[[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil] autorelease]] autorelease];
[self.navigationControllers addObject:_settingsNavigationController];

SummaryViewController 的分配:

self.summaryNavigationController = [[[UINavigationController alloc]
                                             initWithRootViewController:[[[SummaryViewController alloc] initWithNibName:@"SummaryViewController" bundle:nil] autorelease]] autorelease];
[self.navigationControllers addObject:_summaryNavigationController];

SlidingController的Dealloc:

- (void)dealloc
{    
    [self.navigationControllers removeAllObjects];
    self.summaryNavigationController = nil;
    self.settingsNavigationController = nil;
    self.navigationControllers = nil;

    [super dealloc];
}

提前致谢。

4

0 回答 0