Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory management - how best to initialise an instance declared in the header
    primarykey
    data
    text
    <p>I've read a few posts on this, but there's still one thing that's not clear for me. I know this might be rather a n00b question, but I've actually got rather far into development without quite grasping this fundamental issue. A symptom of being self taught I guess. </p> <p>You declare a variable in your header, like so:</p> <pre><code>@interface SomeClass : NSObject { NSMutableArray *anArray; } @property (nonatomic, retain) NSMutableArray *anArray; end </code></pre> <p>And then in your main file you synthesise it and set it to an initial value:</p> <pre><code> @implementation SomeClass @synthesize anArray - (SomeClass *)init{ if (self = [super init]) { self.anArray = [[NSMutableArray alloc] initWithCapacity:10]; } [return self]; </code></pre> <p>And release it when your Class deallocs:</p> <pre><code>- (void)dealloc { [anArray release]; [super dealloc]; } </code></pre> <p>Now, when I run instruments, the line</p> <pre><code>self.anArray = [[NSMutableArray alloc] initWithCapacity:10]; </code></pre> <p>is identified as a memory leak. Is it a memory leak because when you define the variable anArray in the header it allocates memory? (Because I thought it was a null pointer.) Therefore when you want to initialise it, and you call [[NSMutableArray alloc] initWithCapacity:10], you are reallocating the memory, and losing the pointer to the original allocation?</p> <p>So instead, I use the convenience class method:</p> <pre><code>@implementation SomeClass @synthesize anArray - (SomeClass *)init{ if (self = [super init]) { self.anArray = [NSMutableArray arrayWithCapacity:10]; } [return self]; </code></pre> <p>This is no longer identified as a memory leak in instruments. And since it's a convenience method, anArray is autoreleased. However, if I am to assume that the instance declaration in the header allocates memory, which would explain the previous issue, then should I still release anArray? Does setting the initial values in this way retain it perhaps?</p> <p>I understand the difference between </p> <pre><code>NSMutableArray *anArray = [[NSMutableArray alloc] initWithCapacity:10]; </code></pre> <p>and </p> <pre><code>NSMutableArray *anArray = [NSMutableArray arrayWithCapactiy:10]; </code></pre> <p>but what I'm not sure I understand is when you've declared NSMutableArray *anArray in your header, which of the two approaches you should use and why. And whether or not if you use the second approach, you should still release anArray when you call dealloc. </p> <p>I might add that I've found the following posts/links useful:</p> <ul> <li><a href="https://stackoverflow.com/questions/1740286/suggest-the-best-way-of-initialization-of-array-or-other-objects">Suggest the best way of initialization of array ( or other objects ) </a> </li> <li><a href="https://stackoverflow.com/questions/193288/what-is-the-cost-of-using-autorelease-in-cocoa/#193640">What is the cost of using autorelease in Cocoa?</a> </li> <li><a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html" rel="nofollow noreferrer">http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html</a> </li> <li><a href="https://stackoverflow.com/questions/1053592/objective-c-class-vs-instance-methods">What is the difference between class and instance methods?</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload