Andy Jacobs
Andy Jacobs

Reputation: 15245

Initializing MKMapView starts with retain count 2

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?

Upvotes: 0

Views: 467

Answers (4)

Caleb
Caleb

Reputation: 124997

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.

Upvotes: 0

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

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

Upvotes: 4

visakh7
visakh7

Reputation: 26390

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

Upvotes: 0

Terry Wilcox
Terry Wilcox

Reputation: 9040

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?

Upvotes: 0

Related Questions