Re: NSAutoreleasePool Problem
Re: NSAutoreleasePool Problem
- Subject: Re: NSAutoreleasePool Problem
- From: Shawn Erickson <email@hidden>
- Date: Sun, 29 Jan 2006 13:32:16 -0800
On Jan 29, 2006, at 1:17 PM, Matthew Miller wrote:
Hi All -
New to the Cocoa development environment (and Objective-C for that
matter) and I'm curious. If you write a simple app that creates
two instances of NSNumber as follows, you'll get the same address
space.
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSLog(@"Hello, World!");
NSNumber *test = [[NSNumber alloc] initWithInt:10];
NSNumber *test2 = [[NSNumber alloc] initWithInt:10];
printf("%d (%d)\n", [test intValue], [test retainCount]);
printf("%d (%d)\n", [test2 intValue], [test2 retainCount]);
[test release];
[test2 release];
[pool release];
return 0;
}
The output looks like this:
[Session started at 2006-01-29 13:48:10 -0500.]
2006-01-29 13:48:10.578 ArrayWithRetainProb[362] Hello, World!
10 (3)
10 (3)
ArrayWithRetainProb has exited with status 0.
Obviously, I'm missing something but I thought that alloc would
work like new in C++ and assign a new address. BTW, this doesn't
seem to happen when you use NSString. So I'm confused there.
NSNumber has a few numbers that come from a preallocated pool
(believe 0 thru 10) to save space in an applicaton by allowing it to
share commonly used NSNumbers. In other words you are getting a valid
reference to the NSNumber you want but it just happens to be a cached
shared instance. As you see the retain count is three (2 of those are
from your alloc/init and the other is from the cache that holds onto
those handful of shared number instances).
-Shawn
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden