Memory Management in Objective-C [duplicate]

Possible Duplicates:
Learn Obj-C Memory Management
Where are the best explanations of memory management for iPhone?

I come from a C/C++ background and the dynamic nature of Objective-C is somewhat foreign to me, is there a good resource anyone can point me to for some basic memory management techniques in Objective-C? ex. retaining, releasing, autoreleasing

For instance, is it completely illegal to use a pointer to an Objective-C object and treat it as an array? Are you forced to use NSArray and NSMutableArray for data structures?

I know these are pretty newbie questions, thanks for any help you can offer me.


Solution 1:

Here you go:

Application memory management is the process of allocating memory during your program’s runtime, using it, and freeing it when you are done with it. A well-written program uses as little memory as possible. In Objective-C, it can also be seen as a way of distributing ownership of limited memory resources among many pieces of data and code. When you have finished working through this guide, you will have the knowledge you need to manage your application’s memory by explicitly managing the life cycle of objects and freeing them when they are no longer needed.

Although memory management is typically considered at the level of an individual object, your goal is actually to manage object graphs. You want to make sure that you have no more objects in memory than you actually need...

Solution 2:

It is generally not useful to repeat the basic rules of memory management, since almost invariably you make a mistake or describe them incompletely -- as is the case in the answers provided by 'heckj' and 'benzado'...

The fundamental rules of memory management are provided in Apple's documentation in Memory Management Rules.

Apropos of the answer from 'www.stray-bits.com': stating that objects returned from "non-owning" methods are "autoreleased" is also at best misleading. You should typically not think in terms of whether or not something is "autoreleased", but simply consider the memory management rules and determine whether by those conventions you own the returned objet. If you do, you need to relinquish ownership...

One counter-example (to thinking in terms of autoreleased objects) is when you're considering performance issues related to methods such as stringWithFormat:. Since you typically(1) don't have direct control over the lifetime of these objects, they can persist for a comparatively long time and unnecessarily increase the memory footprint of your application. Whilst on the desktop this may be of little consequence, on more constrained platforms this can be a significant issue. It is therefore considered best practice on all platforms to use the alloc/init pattern, and on more constrained platforms, where possible you are strongly discouraged from using any methods that would lead to autoreleased objects.

(1) You can take control by using your own local autorelease pools. For more on this, see Apple's Memory Management Programming Guide.