PDA

View Full Version : when would you use allocWithZone?




Chirone
Nov 14, 2010, 03:55 PM
just wondering when you would use this method over just your normal alloc

i saw the method being used in some of apples sample code related to NSMenu



lee1210
Nov 14, 2010, 04:02 PM
When physical locality in memory for different objects matters. This is probably rare in practice, but if you have a long-running program, it may be important to keep things close together to avoid swapping with closely-related objects. If these objects are far apart physically in memory it may require swapping to access both of them close together temporally.

-Lee

Chirone
Nov 14, 2010, 04:19 PM
So it's for performance reasons then...

would you suggest it be used for menus?

lee1210
Nov 14, 2010, 04:24 PM
I could imagine it being useful for menus, as they will be used together and stick around for the life of your program. I'd keep it simple for now, but if one were to use this, menus might be a good place.

-Lee

Sydde
Nov 14, 2010, 11:46 PM
Zones appear to be largely non-useful. This is what CocoaDev has to say about them (http://www.cocoadev.com/index.pl?NSZone). The biggest drawback to zones is that you cannot apparently set the default zone, and if you use allocWithZone on an object that creates other objects, most or all of those other objects will almost certainly be allocated in the default zone.