memory management before arc

When an iOS application is running, there is a run loop that is continually cycling. This run loop checks for events, like a touch or a time firing. Whenever an event occurs, the application breaks  from the run loop and processes that event by calling the methods you have written in your classes. When your code is finished executing, the application returns to the loop. At the end of the loop, all autoreleased objects are sent the message release. So, while you are executing a method, which may call other methods, you can safely assume that an autoreleased object will not be released.

The return value for autorelease is the instance that is sent the message, so you can chain autorelease messages.

NSObject *x= [[[NSObject alloc] init] autorelease];

//or
Possession *newPossession=
[[self alloc] initWithPossessionName:randomName
                      valueInDollars:randomValue
                        serialNumber:randomSerialNumber];

return [newPossession autorelease];

Here are three memory management facts to remember when working with instances of NSMutableArray

  1. When an object is added to an NSMutableArray, that object is sent the message retain; the array becomes an owner of that object and has a pointer to it.
  2. When an object is removed from an NSMutableArray, that object is sent the message release, the array relinquished ownership of that object and no longer has a pointer to it.
  3. When an NSMutableArray is deallocated, it sends the message release to all of its entries.

another autorelease

- (NSString *) description
{
    NSString *descriptionString = 
    [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, Recorded on %@", possessionName,serialNumber,valueInDollars,dateCreated];

    return [descriptionString autorelease];

}


//or 
- (NSString *) description
{
    return [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, Recorded on %@", possessionName,serialNumber,valueInDollars,dateCreated];

}

set

- (void) setSerialNumber: (NSString *) str
{
    [str retain];
    [serialNumber release];
    serialNumber = str;
}

dealloc

- (void) dealloc
{
    [possessionName release];
    [serialNumber release];
    [dateCreated release];
    [super dealloc];
}

 property

@property (nonatomic, copy) NSString *possessionName;

//the generated setter method

- (void) setPossessionName: (NSString *) str
{
    id t = [str copy];
    [possessionName release];
    possessionName = t;
}

Retain count rules

  • If you create an object using a method whose name starts with alloc or new or contains copy, then you have taken ownership of it. You have a responsibility to release the object when you no longer need it.
  • An object created through any other means --like a convenience method-- is not owned by you.
  • If you don't own an object and you want to ensure its continued existence, take ownership by sending it the message retain.
  • When you own an object and no longer need it, send it the message release or autorelease. (release decrements the retain count immediately. autorelease causes the message release to get sent when the autorelease pool is drained)
  • As long as an object has at least one owner, it will continue to exist.( When its retain count goes to zero, it is sent the message dealloc.)

 

posted on 2012-06-13 09:51  grep  阅读(197)  评论(0编辑  收藏  举报