advertisement

Article:
  Knowing When to Let Go: Better Living Through Memory Management
Subject:   hmmm
Date:   2003-06-20 19:36:37
From:   anonymous2
From the text:


"With this in mind, here's a guideline for the use of -autorelease:
* If you need to defer ownership of an object, autorelease it."


I would suggest that that be re-written as "If you need to defer relinquishment of ownership..." If you have retained an object, you already "own" it; you want to give up ownership at some later time.


In the next paragraph:


"To simplify that, if you want to create an object and give it to something else and then completely forget about it, make sure that it's autoreleased. Another good example is if you want to create an array without having to release every object you add to it after you're done; autorelease, and it's all done for you."


This is a misleading over-simplification, and contradicts your later (itself perhaps over-exaggerated) exhortation to avoid autorelease.


It is perfectly reasonable to simply release an object that has been put into an array. Contrast


aNumber = [NSNumber numberWithInt:n];
[aMutableArray addObject:aNumber];


with


aNumber = [[NSNumber alloc] initWithInt:n];
[aMutableArray addObject:aNumber];
[aNumber release];


The latter is both correct from the perspective of memory management and more efficient.


mmalc




1 to 1 of 1
  1. hmmm
    2003-06-23 09:03:21  anonymous2 [View]

1 to 1 of 1