I am trying to see the logic of one aspect of retain/release for memory management of cocoa.
If an object is added to a collection, ( Dictionary, Array) the retain count ( as I understand it) is incremented automatically for that object, and an autorelease message is sent to that object.
Now, when an object is assigned to a variable, the retain count is unchanged. In one of Kochan's examples, when this assignment occurred, a retain message was sent to the object..which I think I understand.
So, could anyone help me with the **logic** of this approach. I can readily see why a language would automatically retain an object that is added to a collection ( if the initial variable that "owned" that object were to be released, the "collection" object would be deallocated too, which would not be good.
But...what about the straight assignment. If it is customary ( and I am not sure if it is) to "retain" an assigned object "manually" ie by the programmer, why was this not incorporated into the language. In other words, why would one **not** wish to retain an assigned object, and why does the language not do the same as if the object were being stored in an array? ( for example)
Hopefully this makes sense? If not, it is probably because I do not understand the language properly.
thank you as usual.
If an object is added to a collection, ( Dictionary, Array) the retain count ( as I understand it) is incremented automatically for that object, and an autorelease message is sent to that object.
Now, when an object is assigned to a variable, the retain count is unchanged. In one of Kochan's examples, when this assignment occurred, a retain message was sent to the object..which I think I understand.
So, could anyone help me with the **logic** of this approach. I can readily see why a language would automatically retain an object that is added to a collection ( if the initial variable that "owned" that object were to be released, the "collection" object would be deallocated too, which would not be good.
But...what about the straight assignment. If it is customary ( and I am not sure if it is) to "retain" an assigned object "manually" ie by the programmer, why was this not incorporated into the language. In other words, why would one **not** wish to retain an assigned object, and why does the language not do the same as if the object were being stored in an array? ( for example)
Hopefully this makes sense? If not, it is probably because I do not understand the language properly.
thank you as usual.