看了下官方文档的关于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 must not release 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 must not release 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;