Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-06-2009, 12:10 AM   #1 (permalink)
New Member
 
Join Date: Aug 2009
Posts: 2
Default Basic question regarding NSMutableArray

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:

Code:
NSMutableArray *myArray = [[NSMutableArray alloc] init];
while([myArray count] < 10)
{
UIImageView *myImageView = [[UIImageView alloc] init];
[myArray addObject:myImageView];
[myImageView release];
}
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).

Correct me if I am wrong somewhere.
hairyhi is offline   Reply With Quote
Old 08-06-2009, 12:42 AM   #2 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 29
Default

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.
harryvermeulen is offline   Reply With Quote
Old 08-06-2009, 12:43 AM   #3 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

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.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 08-06-2009, 01:11 AM   #4 (permalink)
New Member
 
Join Date: Aug 2009
Posts: 2
Default

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.
hairyhi is offline   Reply With Quote
Old 08-06-2009, 01:21 AM   #5 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 29
Default

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).

If it doesn't make sense, just spend a few minutes reading: Memory Management Programming Guide for Cocoa: Object Ownership and Disposal And the world of memory management will be clear to you
harryvermeulen is offline   Reply With Quote
Reply

Bookmarks

Tags
nsmutablearray

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 255
24 members and 231 guests
ADY, bookesp, chillyh, ckgni, dacapo, Dani77, Davey555, Desert Diva, glenn_sayers, HemiMG, JasonR, LEARN2MAKE, M.A.S., marshusensei, mer10, nobre84, prchn4christ, Raggou, Rudy, ryantcb, Speed, themathminister, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,765
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:36 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0