I have a question related to this piece of code
NSNumber *lastObject = [self.myStack lastObject];
if(lastObject){
[self.myStack removeLastObject];
}
return [lastObject doubleValue];
I am surprised that the lastObject is still in memory despite getting removed.
How is this happening? Is it that lastObject returns a copy of the object?
No, it’s not a copy, it’s just autoreleased. It will be released in the near future.
EDIT: to clarify – on
[self.myStack removeLastObject], the object is sentreleaseimmediately, but you got an autoreleased pointer from[self.myStack lastObject]so the array is not the last owner of the object.