I have a basic question here.
I know that dealloc will be called when the object’s reference count becomes zero,and dealloc releases all the resources hold by the object or frees memory, right?
The object reference count becomes zero if we send release message to that object right?.
Lets consider the following object with its property created as,
@property (retain) NSString* myString;//reference count 1
and dealloc
[myString release];//reference count 0
[super dealloc];
I am not releasing the myString object any where except in dealloc.
My question is who is making myString object reference count to zero so that dealloc will be called?
Please clarify my doubt.
Anything that maintains ownership of the object is responsible for releasing it. For example if the code you posted is the only thing that maintains ownership of the
NSStringstored inmyStringthen when you call release the reference count will be decreased and the object will likely be deallocated (String literals are different). Now if you passedmyStringaround or something else requestedmyStringand retains it then that code is also responsible for releasing it which may be before or after you release it in the dealloc method.I recommend referring to the documentation for reinforcing this concept.