![]() |
| |||||||
|
Tutorials
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { |
||||
| NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // insert code here... NSLog(@"Hello, World!"); [pool release]; return 0; |
||||
| } | ||||
As things stand, half the code (the Autorelease pool) is devoted to memory management, which means this is a good time to go over memory management in Obj-C. Stepwise has a couple of tutorials, and there is another at Cocoa Dev Central, but I'll also give a quick rundown here. Most objects we'll be creating are put by default into an autorelease pool. If we don't do anything else to keep them around, the memory they use gets freed up when the active autorelease pool is purged. In GUI applications, the application itself creates an autorelease pool that's recycled for you as part of the regular workings of the program's runloop (for more on runloops, see the next tutorial). In this way, most memory we use gets taken care of without any programmer intervention.
There are a couple of ways to keep objects around for longer. One is to create them using a method such as "alloc", which keeps the memory around by default. You can also do that explicitly with a "retain" message. These objects will be kept around even if you lose track of them in the program, so using them creates a danger of memory leaks. To get rid of them and keep them from leaking, you need to send those objects a "release" message. As soon as there are a number of release messages equal to the total of the alloc + retains, the object goes into the autorelease pool. Using too many release messages, however, will clear the memory too soon and cause errors, so you have to keep things balanced. For those situations where you need to keep an object around for a breif period (say, returning one from a method), you can also use the "autorelease" message. This will keep the object around a for long enough for some other code to decide whether to retain it or not, then drop it into the autorelease pool.
As I mentioned above, in GUI and other runloop-based applications, autorelease pool creation and destruction are handled for you. Since a tool like this may just execute briefly and then exit without entering into a runloop, autorelease pools need to be created explicitly. There are other cases where you may want to make your own autorelease pool - for example, when in a loop where you create many objects, you can cut down on total memory use by allocating and releasing pools explicitly.
Tommarow in the next section, I'll intentionally make some errors in memory management to give you a sense of how to detect problems and figure out where they occur.
If you have any questions or comments about this article, feel free to e-mail me at john_timmer@osxfaq.com
| Copyright © 2000-2010 Inside Mac Media, Inc. All rights reserved. | ||
| Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users. | ||
| Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc. |