使用pushViewController进行内存管理

ric*_*ich 14 iphone memory-management pushviewcontroller ios

我对内存管理技术的要求还不是很高,并且想知道是否有人可以向我解释这种奇怪的行为.考虑一下我测试的这3段代码:

DofView* dofView = [[DofView alloc] initWithNibName:@"DofView" bundle:nil];
NSLog(@"dof retain count = %d", [dofView retainCount]); 
Run Code Online (Sandbox Code Playgroud)

此日志:保留计数= 1.这很好.

DofView* dofView = [[DofView alloc] initWithNibName:@"DofView" bundle:nil];
[dofView release];
NSLog(@"dof retain count = %d", [dofView retainCount]); 
Run Code Online (Sandbox Code Playgroud)

这个日志:保留计数= 1.不应该是0 ??

DofView* dofView = [[DofView alloc] initWithNibName:@"DofView" bundle:nil];
[self.navigationController pushViewController:dofView animated:YES];
NSLog(@"dof retian count = %d", [dofView retainCount]);
Run Code Online (Sandbox Code Playgroud)

这个日志:保留计数= 5.我不知道为什么它的五个?

任何人都对此有所了解吗?我担心每次创建新视图时我都会吃掉内存.

谢谢!

Ale*_*lds -1

release调用不会立即生效,因此保留计数可能看起来不正确。您可以使用 Leaks 工具来跟踪内存问题,但只要遵循内存管理规则,一般来说应该没问题。

  • 发布立即生效。自动释放需要一段时间才能发生。keepCount _looks_ 不正确的原因是因为您不知道对象/框架内发生了什么。- 它可能被其他东西保留。 (3认同)