The iPhone OS doesn't support garbage collection because of power considerations. That means every object that's created must eventually have its memory released by hand - at least, if you don't want to introduce a memory leak into your program. The fundamental rule of memory management in the iPhone OS is this: if you allocate memory for an object, you must release it. This is done via the release message:
[object release];
Send that message when you're all done using an object, and you've done your proper duty as a programmer. Note that we said you only must release the memory if you allocated the memory for it. You don't allocate the memory for those, which means you're not responsible for releasing it. Instead, the class object that does the creation has to clean up its memory. How does the OS know when you've finished working with the object it created for you? That's done through the wonders of autorelease.
If you're responsible for the creation of an object and you're going to pass it off to some other class for usage, you should autorelease the object before you send it off. This is done with the autorelease method:
[object autorelease];
You'll typically send the autorelease message just before you return the object at the end of a method. After an object has been autoreleased, it's watched over by a special NSAutoreleasePool. The object is kept alive for the scope of the method to which it's been passed, and then the NSAutoreleasePool cleans it up.
What if you want to hold onto an object that has been passed to you and that will be autoreleased? In that case, you send it a retain message:
[object retain];
When you do this, you're saying you want the object to stay around, but now you've become responsible for its memory as well: you must send a release message at some point to balance your retain. At this point, we should probably back up and explain the underlying way that the iPhone OS manages memory objects. It does so by maintaining a count of object usage. By default, it's set to 1. Each retain message increases that count by 1, and each release message reduces that count by 1. When the count drops to 0, the memory for the object is freed up. Therefore, all memory management can be thought of as pairs of messages. If you balance every alloc and every retain with a release, your object will eventually be freed up when you're done with it.
Our website is not responsible for the information contained by this article. Webworldarticles.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.
This article was sent to us by:
Christian Glousten at
09232010
1. Market segmentation is a necessity when selling an iPhone iPad app
All articles in this directory are property of their respective authors. Additionally, read our Privacy Policy
© 2010 WebWorldarticles.com - All Rights Reserved. Partners: Gunblade Saga