best way to "reset" values

  • Hi All,

    As I grapple with the whole memory management issue, a question I
    haven't seen explicitly addressed is now to reset ivars to default
    (empty) values.

    As far as I understand, if I use

    [self setMyString: @""];

    then the @"" is a constant that can never be freed. Or is that OK? (I
    need to reset the value often, and don't want to leak memory.) Also,
    is the approach different if the ivar is a collection (set, array,
    dictionary...)?

    Thanks.

    Daniel
  • On Sep 1, 2007, at 9:00 AM, Daniel Child wrote:

    > Hi All,
    >
    > As I grapple with the whole memory management issue, a question I
    > haven't seen explicitly addressed is now to reset ivars to default
    > (empty) values.
    >
    > As far as I understand, if I use
    >
    > [self setMyString: @""];
    >
    > then the @"" is a constant that can never be freed. Or is that OK?
    > (I need to reset the value often, and don't want to leak memory.)

    Look at it from the point of view of the Cocoa memory contract "rules".

    Does " @"" " contain the words alloc, copy, new, etc.? Nope... so you
    don't have to worry about memory managing that object. It is also
    true that it is an immutable string (like NSString is) the rest is,
    in reality, an implementation detail.

    > Also, is the approach different if the ivar is a collection (set,
    > array, dictionary...)?

    Read the memory management rule... it is hard to answer your question
    in the abstract and do it as well as the document I referenced
    earlier does.

    -Shawn
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today