Hi everyone,
I am new to the Mac/iphone development world. So please correct me if I am going wrong somewhere. I have a very basic question regarding the usage of NSMutableArray. I'll be able to explain this using this piece of code:
Here I dynamically allocate some memory while initializing the mutable array. Then allocate memory for each UIImageView object, assign that memory (in form of object) to myArray and then release it. Here I have 2 doubts
If I am releasing (freeing) the memory of the UIImageView object, how is it still accessable from the myArray.
And what memory is actually allocated from NSMutableArray alloc (because as far as I understand this NSMutableArray is just a pointer to a dynamically allocated object (which is initialized separately).
in objective-c release doesn't mean 'throw away' or 'dispose'. you release control over the memory allocation by that particular object, but another object (your array) might still have control over the memory. As long as there is an object with control over the memory allocation, it won't be freed or thrown away.
My explanation is not completely accurate, so read up on some of the memory management documents apple provides on the developer pages for a better and more complete understanding.
Objects are referenced by pointer, and kept alive by what is called a retain count. When you create your image, it starts with a retain count of 1, which means you own it and are responsible for sending a release message when you are done with it. When you add it to the array, what you are really doing is adding its pointer; and since the array now needs the item, the array will retain it, bringing your retain count total to 2.
When you send a release message, that causes a -1 on the retain count; it does NOT kill the object. Objects are destroyed when the retain count reaches zero. This will not happen yet because the array is still using the object.
At some point, you will need to release the array. When the array's retain count goes to zero, it will be deallocated; during that process, it will send a release message to each of the objects in the array. That will bring your image down to zero, at which point it will be deallocated.
This same basic premise applies to pretty much all of the collection types - array, dictionary, etc.
Hi folks thanks for the info on it..
So according to this, if I define a dealloc method in a class, is this called for every release we do (rather dereferencing) or only when finally the memory is freed.
Also to competely deallocate/release the NSMutableArray and its memory, do I have to deallocate all the objects in the array or a simple "[myArray release]" will do all the task?
And lastly (doubt no. 2 from my first post on the thread), what does ][NSMutableArray alloc] init] do?
Now I think it allocates memory for the array of pointers(which will eventually hold the objects), but I am not sure as the size of array is undefined.
The concept of memory management is just different in objective-c. You shouldn't worry about freeing memory, instead worry about ownership of memory. Like BrainSlick said, once the retain count (ownership count) of a piece of memory becomes 0, it will be freed. So again, [myArray release] doesn't free the memory, it merely releases the array's control over the memory. If there are other objects still in control of the same memory, the memory will retain(pun intended).