Initializing MKMapView starts with retain count 2

流过昼夜 提交于 2019-12-12 06:28:25

问题


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?


回答1:


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).




回答2:


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?




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/5743899/initializing-mkmapview-starts-with-retain-count-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!