• 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: A few questions about Memory Management
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A few questions about Memory Management


  • Subject: Re: A few questions about Memory Management
  • From: Charlton Wilbur <email@hidden>
  • Date: Sat, 12 Feb 2005 19:50:25 -0500


On Feb 12, 2005, at 10:55 AM, Adrian R. Foltyn wrote:

But still i have a problem with this. Let me explain.

Example code:
NSArray *allFileContents = [[NSArray arrayWithObject:[NSString stringWithContentsOfFile:@"/some/path"]] retain];



Step 1:
NSString instance is created with the method stringWithContentsOfFile: and has a retain count of 1. Because no alloc, copy or mutableCopy method was used the instance is added to the autorelease pool. Objects added to the autorelease pool receive a release message. Thus the NSString instance has now a retain count of 0.

Objects added to the autorelease pool receive a release message *later*. When the string is returned, it has a retain count of 1, and it is guaranteed to receive a release message later.


Step 2:
NSArray instance is created with the method arrayWithObject: and has a retain count of 1. Because no alloc, copy or mutableCopy method was used the instance is also added to the autorelease pool and now has a retain count of 0.

Again, retain count 1, pending autorelease count 1.

Step 3:
NSString instance is being added to the array thus receiving a retain message and raising its retain count to 1.

Retain count 2, pending autorelease count 1.

Step 4:
NSArray instance receives a manual retain message and has a retain count of 1.

Retain count 2, pending autorelease count 1.

Step 5:
The thread ends and sends a release message to the autorelease pool.

Step 6:
The pool sends a release message to each object it contains.

What bothers me now is that the NSString array was created first and so comes first in the list of objects in the autorelease pool. Either NSAutoreleasePool is smart enough to handle this or the following actions would occur:

NSAutoreleasePool sends a release message to the NSString instance first thus setting the retain count to 0. Then the pool sends a release message to the NSArray instance and sets its retain count to 0. The array sends a release message to its member thus setting the retain count of the NSString instance to -1?

The order doesn't matter if you've correctly balanced your retains and releases and made sure that you retain any object you have an interest in.


At this point, you have two objects. Each has a retain count of 2, and a pending autorelease count of 1. When the autorelease pool sends the message, you will then have two objects with a retain count of 1 and a pending autorelease count of 0. The string object received one retain from the array it's in, so it's the array's responsibility to release it when it's removed; you retained the array, so you have to make sure you release it when you're done with it.

Your mistake is that you're counting the autorelease twice, once in step 1 or step 2 and once in step 6.

Charlton


-- Charlton Wilbur email@hidden email@hidden

_______________________________________________
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


  • Follow-Ups:
    • Re: A few questions about Memory Management
      • From: "Adrian R. Foltyn" <email@hidden>
References: 
 >A few questions about Memory Management (From: "Adrian R. Foltyn" <email@hidden>)
 >Re: A few questions about Memory Management (From: Charlton Wilbur <email@hidden>)
 >Re: A few questions about Memory Management (From: "Adrian R. Foltyn" <email@hidden>)

  • Prev by Date: Re: Objective-C BufferedReader
  • Next by Date: Re: Pure Quartz vs. Cocoa-Objects ... why is Quartz slower?
  • Previous by thread: Re: A few questions about Memory Management
  • Next by thread: Re: A few questions about Memory Management
  • Index(es):
    • Date
    • Thread