Women in Technology

Hear us Roar



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



Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • hmmm
    2003-06-23 09:03:21  anonymous2 [View]

    You are of course completely correct, and it's nice to have your comments on it, but it still violates the DoTheSimplestThingThatCouldPossiblyWork philosophy (as featured on the Portland Pattern Repository).

    For simple programs, autoreleasing in this sort of situation is not just convenient but probably preferable since you don't need the gain in efficiency.

    I suppose I had better restate the ideal:
    In programs where efficiency is key, avoid autoreleasing if possible. In programs where simplicity is key, autorelease where the convenience helps.

    Your mileage may vary; strike your own balance.