看了下官方文檔的關于object c 的內存管理,總結下:
-
You own any object you create by allocating memory for it or copying it.
Related methods:alloc,allocWithZone:,copy,copyWithZone:,mutableCopy,mutableCopyWithZone:
-
If you are not the creator of an
object, but want to ensure it stays in memory for you to use, you can
express an ownership interest in it.
Related method:retain
-
If you own an object, either by
creating it or expressing an ownership interest, you are responsible for
releasing it when you no longer need it.
Related methods:release,autorelease
-
Conversely, if you are not the creator of an object and have not expressed an ownership interest, you mustnotrelease it.
You own any object you create by allocating memory for it or copying it.
Related methods:alloc,allocWithZone:,copy,copyWithZone:,mutableCopy,mutableCopyWithZone:
If you are not the creator of an object, but want to ensure it stays in memory for you to use, you can express an ownership interest in it.
Related method:retain
If you own an object, either by creating it or expressing an ownership interest, you are responsible for releasing it when you no longer need it.
Related methods:release,autorelease
Conversely, if you are not the creator of an object and have not expressed an ownership interest, you mustnotrelease it.
首先我們不是sprockets的 creater.(第一條規則),我們也沒有expressing an ownership interest,因為我們沒有
retain它,(第二條規則) 所以我們不負責release它。
具體來看thingamajig的sprockets方法的實現:
array = [[NSArray alloc] initWithObjects:mainSprocket,auxiliarySprocket, nil];
return [array autorelease];
}
}
[count release];
count = newCount;