[Openmcl-devel] Allocating temporary nsstrings

Raffael Cavallaro raffaelcavallaro at mac.com
Sun Mar 13 19:02:24 UTC 2011


On Mar 13, 2011, at 10:50 AM, Paul Krueger wrote:

>  The fact that the caller is not responsible for objects returned by class constructors is not especially well-documented

Just for completeness, the whole Cocoa memory management scheme/disaster is documented here:

<http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html>

and the section on Convenience Constructors is in that article in the section "Practical Memory Management":

<http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html>


The usual shorthand rule is called the CAR rule; you only call #/release or #/autorelease on objects you "own" which is to say, only call #/release or #/autorelease on objects you get by calling one of:

1. C for #/copy
2. A for #/alloc and #/new (or any method beginning with "alloc" or "new")
3. R for #/retain

Everything else either belongs to some other object which will release it when its parent object is released (e.g., objects in an NSArray), or has already been autoreleased, for example, because it was created by a Convenience Constructor.

Finally, if you're creating a large number of temporary objects, say in a loop, or a smaller number of very large temporary objects (e.g., NSImage), to reduce maximum memory footprint you probably want to create and drain/release a new autorelease pool each time through the loop or each time you use a large temp object, rather than performing the whole loop or processing all of the large temp objects inside a single autorelease pool.


warmest regards,

Ralph







Raffael Cavallaro
raffaelcavallaro at me.com








More information about the Openmcl-devel mailing list