• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSDictionary, allKeys and the NSAutoreleasePool
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDictionary, allKeys and the NSAutoreleasePool


  • Subject: Re: NSDictionary, allKeys and the NSAutoreleasePool
  • From: Greg Parker <email@hidden>
  • Date: Thu, 3 Sep 2009 13:12:00 -0700

On Sep 3, 2009, at 11:47 AM, Jens Alfke wrote:
On Sep 3, 2009, at 11:40 AM, Scott Andrew wrote:
I could have sworn in either the apple docs (or one of the cocoa books, maybe this board). I maybe thinking of the general rule that you don't own the object until you retain it and its only valid or the within the method that its recieved. My bad.

Here's an example of a return value that isn't autoreleased:

- (Foo*) foo {
	static Foo* sharedFoo;
	if (!sharedFoo) sharedFoo = [[Foo alloc] init];
	return _sharedFoo;
}

Something similar is exactly what happens in the original code. On Snow Leopard at least, both NSDictionary and NSArray have optimizations for the "empty immutable collection" case. In both classes there's a single shared instance that lives forever, so it won't be destroyed even after every retain count that you are responsible for has been released.


Try this:

{
    NSDictionary *di = [[NSDictionary alloc] init];
    NSArray      *ar = [di allKeys];
    NSDictionary *di2 = [[NSDictionary alloc] init];
    NSArray      *ar2 = [di allKeys];

    NSLog(@"di %p, di2 %p", di, di2);
    NSLog(@"ar %p, ar2 %p", ar, ar2);
}

You'll see that there's only one NSDictionary object and one NSArray object, even though you called +alloc twice.

Then try your original test with a non-empty NSDictionary. You'll see the memory management behave more like you expect. (Of course, `[ar retainCount]` is likely to crash if called after `ar` is deallocated.)


-- Greg Parker email@hidden Runtime Wrangler


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >NSDictionary, allKeys and the NSAutoreleasePool (From: Horst Jäger <email@hidden>)
 >Re: NSDictionary, allKeys and the NSAutoreleasePool (From: Scott Andrew <email@hidden>)
 >Re: NSDictionary, allKeys and the NSAutoreleasePool (From: mmalc Crawford <email@hidden>)
 >Re: NSDictionary, allKeys and the NSAutoreleasePool (From: Scott Andrew <email@hidden>)
 >Re: NSDictionary, allKeys and the NSAutoreleasePool (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: watch changes to any properties on an object
  • Next by Date: RE: NSServices
  • Previous by thread: Re: NSDictionary, allKeys and the NSAutoreleasePool
  • Next by thread: Re: NSDictionary, allKeys and the NSAutoreleasePool
  • Index(es):
    • Date
    • Thread