初始化 MKMapView 从保留计数 2 开始

发布于 2024-11-02 16:58:59 字数 768 浏览 2 评论 0原文

我已将 MKMapView 添加到我的应用程序中,但是当我将地图分配到内存中时,它以保留计数 2 开始(我使用 iOS 4.0 作为基本 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 已经创建的预定义对象?

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]);

The output show this result

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苍景流年 2024-11-09 16:58:59

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

请阅读下面 Apple 关于 retainCount 的说法>。

重要提示:此方法通常对于调试内存管理问题没有任何价值。因为任意数量的框架对象可能保留了一个对象以保存对其的引用,而同时自动释放池可能在对象上保存任意数量的延迟释放,因此您不太可能从中获得有用的信息方法。

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

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

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

Shark(请参阅 Shark 用户指南)还分析内存分配(以及程序的许多其他方面)。

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

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:

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

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

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

风筝在阴天搁浅。 2024-11-09 16:58:59

不要看保留计数,你只会感到困惑。坚持遵循内存管理规则并假装保留计数不存在。

Instruments 是否说它泄漏了?

Don't look at retain counts, you'll just get confused. Stick with following the memory management rules and pretend retain count doesn't exist.

Does Instruments say it's leaking?

九命猫 2024-11-09 16:58:59

如果您不通过查看 retainCount 来尝试了解内存管理,这会对您自己大有裨益。 Apple 关于 内存管理 的参考资料会有所帮助你。不用担心 retainCount 是什么;只需确保释放您拥有所有权的对象,并且不要释放您不拥有的对象。请查看对象所有权

It would do yourself a world of good if you don't look at retainCount trying to learn about the memory management. The Apple reference on Memory Management would help you.Don't worry about what the retainCount is; just make sure that you release objects that you have ownership and don't release objects you don't own. Pls take a look at Object Ownership

漫漫岁月 2024-11-09 16:58:59

保留计数内存模型的全部要点是,您只关心自己的职责,而其他对象只担心他们的职责。如果您正在查看 -retainCount,那么您正在对其他对象正在执行的操作做出(可能是无效的)假设。

也就是说,您所看到的原因是其他一些对象也保留了地图视图。这可能是地图视图本身,或者您不知道的某些内部地图视图管理器,或者其他东西。

The whole point of the retain counted memory model is that you only worry about your own responsibilities, and other objects worry only about theirs. If you're looking at -retainCount, you're making (probably invalid) assumptions about what other objects are doing.

That said, the reason for what you're seeing is that some other object has also retained the map view. That may be the map view itself, or some internal map view manager that you don't know about, or something else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文