YesNSMutableArray对象调用copy返回的是一个NSArray对象,所以在[array4 release]被调用时array4的dealloc方法被调用,输出NSArray dealloc.
Then the autoreleasepool is destroyed, array3 is released, and the dealloc method of array3 is called. Because [super dealloc] is called in the dealloc method of NSMutableArray, the following two sentences are output.
According to the answer on StackOverflow, this is because NSArray is an immutable object, and the reference count of this empty array instance will be increased by [NSArray array]或者[[NSArray alloc] init]生成的都是不可变的空数组,所以苹果默认所有不可变空数组的引用都指向一个唯一实例以进行优化,所以在[NSArray array]之前,这个实例的retainCount就是1了。在代码中不论是[NSArray array]或者[[NSArray alloc] init].
The following code can reflect this more intuitively, with a无关的[NSArray array]语句增加了areference counting.
Answer the first question
The object's memory has been reclaimed during output.
When sending a retainCount message to an object that has been recycled, the output result should be uncertain. If the memory occupied by the object is reused, it may cause the program to crash abnormally.
Why is this indeterminate value 1 instead of 0? Because when release was executed for the last time, the system knew that the memory was about to be recycled, so there was no need to decrement release时,系统知道马上要回收内存,就没必要将retainCount by 1, because the object would definitely be recycled regardless of whether it was decremented by 1 or not.
Not changing this value from 1 to 0 can reduce one memory operation and speed up object recycling.
Conclusion: Don't send messages to objects that have been released.
Extraordinary melon seeds’ answer is right.
Yes
NSMutableArray
对象调用copy
返回的是一个NSArray对象,所以在[array4 release]
被调用时array4的dealloc方法被调用,输出NSArray dealloc
.Then the autoreleasepool is destroyed, array3 is released, and the dealloc method of array3 is called. Because
[super dealloc]
is called in the dealloc method of NSMutableArray, the following two sentences are output.According to the answer on StackOverflow, this is because NSArray is an immutable object, and the reference count of this empty array instance will be increased by
[NSArray array]
或者[[NSArray alloc] init]
生成的都是不可变的空数组,所以苹果默认所有不可变空数组的引用都指向一个唯一实例以进行优化,所以在[NSArray array]
之前,这个实例的retainCount就是1了。在代码中不论是[NSArray array]
或者[[NSArray alloc] init]
.The following code can reflect this more intuitively, with
a
无关的[NSArray array]
语句增加了a
reference counting.If ARC is not turned on, memory management only needs to follow the four "golden rules" in the basic memory management rules in Apple documents.
Answer the first question
The object's memory has been reclaimed during output.
When sending a
retainCount
message to an object that has been recycled, the output result should be uncertain. If the memory occupied by the object is reused, it may cause the program to crash abnormally.Why is this indeterminate value 1 instead of 0? Because when
release
was executed for the last time, the system knew that the memory was about to be recycled, so there was no need to decrementrelease
时,系统知道马上要回收内存,就没必要将retainCount
by 1, because the object would definitely be recycled regardless of whether it was decremented by 1 or not.Not changing this value from 1 to 0 can reduce one memory operation and speed up object recycling.
Conclusion: Don't send messages to objects that have been released.