初始化MKMapView从保留计数2开始 [英] Initializing MKMapView starts with retain count 2

查看:65
本文介绍了初始化MKMapView从保留计数2开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经向应用程序中添加了MKMapView,但是当我将地图分配到内存中时,它的保留计数为2(我使用iOS 4.0作为基本SDK)

I've added a MKMapView to my application, but when i allocate the map into the memory it starts with a retain count of 2 (i'm using iOS 4.0 as base SDK)

MKMapView *x = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 370)];

NSLog(@"map retain count: %i", [x retainCount]);

[self addSubview:x];


NSLog(@"map retain count: %i", [x retainCount]);

[x release];


NSLog(@"map retain count: %i", [x retainCount]);

[x removeFromSuperview];


NSLog(@"map retain count: %i", [x retainCount]);

输出显示此结果

2011-04-21 14:09:06.159 xx[7373:207] map retain count: 2
2011-04-21 14:09:06.159 xx[7373:207] map retain count: 3
2011-04-21 14:09:06.159 xx[7373:207] map retain count: 2
2011-04-21 14:09:06.160 xx[7373:207] map retain count: 1

最后一个日志的保留计数应该为0,对吗? 还是使用api已经创建的预定义对象?

The retain count should be 0 at the last log right? Or does it use a predefined object that the api already created?

推荐答案

您不应该依赖retainCount,因为有可能由您创建的iOS保留对象,

you should not rely on the retainCount because there are possibility of retaining the object by iOS which is created by you,

在下面阅读 Apple 关于retainCount的说法.

Read below what Apple say about retainCount.

重要:此方法通常在调试内存管理问题时没有任何价值.由于可能有许多框架对象都保留了一个对象以保留对它的引用,而同时自动释放池可能在一个对象上保留了任何数量的延迟发布,因此从此获取有用信息的可能性很小方法.

Important: This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

要了解必须遵守的内存管理基本规则,请阅读内存管理规则" .要诊断内存管理问题,请使用合适的工具:

To understand the fundamental rules of memory management that you must abide by, read "Memory Management Rules". To diagnose memory management problems, use a suitable tool:

LLVM/Clang静态分析器通常甚至可以在运行程序之前就发现内存管理问题.

The LLVM/Clang Static analyzer can typically find memory management problems even before you run your program.

Instruments应用程序中的Object Alloc工具(请参阅《 Instruments用户指南》)可以跟踪对象分配和销毁.

The Object Alloc instrument in the Instruments application (see Instruments User Guide) can track object allocation and destruction.

Shark(请参阅《 Shark用户指南》)还介绍了内存分配(在程序的许多其他方面).

Shark (see Shark User Guide) also profiles memory allocations (amongst numerous other aspects of your program).

这篇关于初始化MKMapView从保留计数2开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆