Disclaimer: I'm pretty new to Objective-C
I'm in a command-line project ARC is NOT enabled
I have a class called MyClass
@interface MyClass : NSObject
@end
@implementation MyClass
@end
and my main looks like
int main(int argc, char *argv[])
{
MyClass *first = [MyClass new];
MyClass *second = first;
return 0;
}
Questions:
I know that *first has a retain count of 1. But I don't understand why *second has retain count = 1 as well? I haven't done new, retain, alloc, or copy on that object.
Since *first has retain count of 1, do I have to call release on that? As you can see in the code, I have to release on the object, but Performance Analyzer shows no Memory-leak. Why?
I noticed that both *first and *second has the same value of memory address. Then I suppose that *first retain count should increase to 2 on assigning. But it does not, why?
I noticed that when I retain *first and assign it to *second both of them have retain count of 2 (see below)
int main(int argc, char *argv[]) { MyClass *first = [MyClass new]; [first retain]; MyClass *second = first; return 0; }So basically *second becomes a COPY of *first in that case when is it released?
What happens to the retain count of both pointers if I change the code to
int main(int argc, char *argv[]) { MyClass *first = [MyClass new]; MyClass *second = first; [first retain]; return 0; }I mean will *second retain count increase as well?
Thank you in advance.